【问题标题】:jar could not find main class, despite manifest尽管清单,jar 找不到主类
【发布时间】:2011-08-13 03:47:24
【问题描述】:

我正在尝试将我的 Java 小程序放入 .Jar 中,以便对其进行签名,因为目前它在本地工作,但是当我尝试远程运行它时抛出拒绝访问异常(它读取目录中的其他文件)。

我在创建jar时正确创建了manifest文件并检查了它:

Manifest-Version: 1.0
Created-By: 1.6.0_25 (Sun Microsystems Inc.)
Main-Class: netApp

netApp 是一个小程序,运行良好,它确实包含一个 main 方法:

import java.awt.*;

import jv.geom.PgElementSet;
import jv.object.PsMainFrame;
import jv.project.PvDisplayIf;
import jv.viewer.PvViewer;
import jv.loader.PgJvxLoader;
import jv.project.PgJvxSrc;
import jv.project.PjProject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import jv.loader.PjImportModel;
import jv.project.PjProject;
import jv.project.PgGeometry;
import jv.viewer.PvViewer;


import jv.object.PsDebug;

import java.applet.Applet;

public class netApp extends Applet {
    public      Frame               m_frame         = null;
    protected   PvViewer            m_viewer;
    protected   PgGeometry          m_geom;
    protected   netAppProj          myModel;

    public void init() {
        // Create viewer for viewing 3d geometries. References to the applet and frame
        // allow JavaView to decide whether program runs as applet or standalone application,
        // and, in the later case, it allows to use the frame as parent frame.
        m_viewer = new PvViewer(this, m_frame);
        //myModel.addActionListener();
        // Create and load a project which contains the user application. Putting code
        // in a JavaView project allows to reuse the project in other applications.
        myModel = new netAppProj();
        m_viewer.addProject(myModel);
        //myModel.start();
        m_viewer.selectProject(myModel);

        setLayout(new BorderLayout());
        // Get 3d display from viewer and add it to applet
        add((Component)m_viewer.getDisplay(), BorderLayout.CENTER);
        add(m_viewer.getPanel(PvViewer.PROJECT), BorderLayout.EAST);
        m_viewer.showPanel(PvViewer.MATERIAL);


        // Get default display from viewer
        PvDisplayIf disp = m_viewer.getDisplay();
        // Register geometry in display, and make it active.
        // For more advanced applications it is advisable to create a separate project
        // and register geometries in the project via project.addGeometry(geom) calls.
        disp.addGeometry(m_geom);
        disp.selectGeometry(m_geom);
        //disp.addPickListener(myModel);
        /*until here */


    }

    /**
     * Standalone application support. The main() method acts as the applet's
     * entry point when it is run as a standalone application. It is ignored
     * if the applet is run from within an HTML page.
     */
    public static void main(String args[]) {
        netApp app  = new netApp();
        // Create toplevel window of application containing the applet
        Frame frame = new jv.object.PsMainFrame(app, args);
        frame.pack();
        // Store the variable frame inside the applet to indicate
        // that this applet runs as application.
        app.m_frame = frame;
        app.init();
        // In application mode, explicitly call the applet.start() method.
        app.start();
        // Set size of frame when running as application.
        netAppProj myModel = new netAppProj();
        frame.setSize(640, 550);        frame.setBounds(new Rectangle(420, 5, 640, 550));

        frame.setVisible(true);
    }
    /** Print info while initializing applet and viewer. */
    public void paint(Graphics g) {
        g.setColor(Color.blue);
        //g.drawString("Loading Geometry Viewer Version: "+PsConfig.getVersion(), 20, 40);
        g.drawString("Loading Projects .....", 20, 60);
    }

    /**
     * Does clean-up when applet is destroyed by the browser.
     * Here we just close and dispose all our control windows.
     */
    public void destroy()   { m_viewer.destroy(); }

    /** Start viewer, e.g. start animation if requested */
    public void start()     { m_viewer.start(); }

    /** Stop viewer, e.g. stop animation if requested */
    public void stop()      { m_viewer.stop(); }
}

我在创建 jar 时尝试了所有方法,包括只做一个:

jar cfm app.jar Manifest.txt *.*

当我尝试从 Windows 资源管理器或通过运行运行 jar 时:

java -jar app.jar

它失败了。出现一般错误:

Could not find the main class: netApp. Program will exit.

netApp.class 肯定在 Jar 中。

提前感谢您的帮助。

【问题讨论】:

  • jar -tf app.jar 列出了什么?
  • netApp.class 在“jar tvf app.jar”的列表中,还有很多东西,因为我使用了 star.star
  • 您能否提供完整的转储——包括与查找清单文件位置相关的路径。
  • 字符限制但这里有一些:C:\Users\Simon\Downloads\files\diagram - 已编辑>jar -tf app.jar META-INF/ META-INF/MANIFEST.MF jars /jars/browserProps.html jars/contact.html jars/copyright.html jars/functions.php jars/help.html jars/javaview.jar jars/jvLite.jar jars/systemFonts.html jars/vgpapp.jar mainClass Manifest.text Manifest.txt models/models/net.jvx myApp.html myApp.jar myApp2.jar myApplet.java MyApplication.class MyApplication.java MyJar MyJar.jar MyProject.class MyProject.java MySurface.class MySurface.java nesdfsdt.jvx net.jvx netApp.class netApp.html netApp.java

标签: java jar applet executable-jar jar-signing


【解决方案1】:

鉴于错误消息,它似乎正确获取了清单,但不是类本身。跑jar tvf app.jar看看。

你的jar 命令在我看来有点不对劲……不应该是这样

jar cfm app.jar manifest.txt *.*

?

【讨论】:

  • netApp.class 在“jar tvf app.jar”的列表中,还有很多东西,因为我使用了 star.star
  • 你是对的,我错误地引用了我的“jar”命令,但我实际上运行了你给出的确切命令
  • @user475989:如果你运行java -classpath app.jar netApp会发生什么?
  • 这是一个很好的尝试——如果 jar 结构正确,那么它应该启动应用程序(或执行 netApp 类中的 main 函数)。如果这失败了,那么你的 jar 已经损坏了
  • 感谢您的帮助,我已经弄清楚了。所需文件之一本身就是一个 jar 文件。我提取了这个,现在它可以工作了。原来你不能在一个罐子里有一个罐子...... I N C E P T I O N
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 2018-01-20
  • 2017-12-10
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多