简体   繁体   English

JAR 文件主类未找到和 java.io.fileNotFoundException

[英]JAR file Main class not found and java.io.fileNotFoundException

public class myapp extends Frame {

    public static void main(String args[]) {
        myapp mdi = new myapp();
    }

    static void Fatal(String s) {
        MessageBox mb = new MessageBox("JMF Error", s);
    }

    JMFrame jmframe = null;
    JDesktopPane desktop;
    FileDialog fd = null;
    CheckboxMenuItem AutoLoop = null;
    Player player = null;
    Player newPlayer = null;
    String filename;

    public myapp() {
        super("Java Media Player");

        // Add the desktop pane
        setLayout(new BorderLayout());
        desktop = new JDesktopPane();
        desktop.setDoubleBuffered(true);
        add("Center", desktop);
        setMenuBar(createMenuBar());

        setSize(640, 480);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

    }

    private MenuBar createMenuBar() {
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String command = ae.getActionCommand();
                if (command.equals("Open")) {
                    if (fd == null) {
                        fd = new FileDialog(myapp.this, "Open File",
                                FileDialog.LOAD);
                    }
                    fd.show();
                    if (fd.getFile() != null) {
                        String filename = fd.getDirectory() + fd.getFile();
                        openFile("file:" + filename);
                    }
                } else if (command.equals("Exit")) {
                    dispose();
                    System.exit(0);
                }
            }
        };

        MenuItem item;
        MenuBar mb = new MenuBar();

        Menu mnFile = new Menu("File");
        item = new MenuItem("Open");
        mnFile.add(item);
        item.addActionListener(al);
        mnFile.add(item = new MenuItem("Exit"));
        item.addActionListener(al);

        // Options Menu
        Menu mnOptions = new Menu("Options");
        AutoLoop = new CheckboxMenuItem("Auto replay");
        AutoLoop.setState(true);
        mnOptions.add(AutoLoop);

        Menu mn = new Menu("Help");
        MenuItem ball = new MenuItem("Build");
        ball.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(myapp.this,
                        "DEVELOPED BY DVM XLADOR");
            }
        });
        mn.add(ball);
        mb.add(mn);
        mb.add(mnFile);
        mb.add(mnOptions);
        mb.add(mn);
        return mb;
    }

    /**
     * Open a media file.
     */
    public void openFile(String filename) {
        String mediaFile = filename;
        Player player = null;
        // URL for our media file
        URL url = null;
        try {
            // Create an url from the file name and the url to the
            // document containing this applet.
            if ((url = new URL(mediaFile)) == null) {
                Fatal("Can't build URL for " + mediaFile);
                return;
            }

            // Create an instance of a player for this media
            try {
                player = Manager.createPlayer(url);
            } catch (NoPlayerException e) {
                Fatal("Error: " + e);
            }
        } catch (MalformedURLException e) {
            Fatal("Error:" + e);
        } catch (IOException e) {
            Fatal("Error:" + e);
        }
        if (player != null) {
            this.filename = filename;
            JMFrame jmframe = new JMFrame(player, filename);
            desktop.add(jmframe);
        }
    }
}

class JMFrame extends JInternalFrame implements ControllerListener {
    Player mplayer;
    Component visual = null;
    Component control = null;
    int videoWidth = 0;
    int videoHeight = 0;
    int controlHeight = 30;
    int insetWidth = 10;
    int insetHeight = 30;
    boolean firstTime = true;

    public JMFrame(Player player, String title) {
        super(title, true, true, true, true);
        getContentPane().setLayout(new BorderLayout());
        setSize(320, 10);
        setLocation(50, 50);
        setVisible(true);
        mplayer = player;
        mplayer.addControllerListener((ControllerListener) this);
        mplayer.realize();
        addInternalFrameListener(new InternalFrameAdapter() {
            public void internalFrameClosing(InternalFrameEvent ife) {
                mplayer.close();
            }
        });

    }

    public void controllerUpdate(ControllerEvent ce) {
        if (ce instanceof RealizeCompleteEvent) {
            mplayer.prefetch();
        } else if (ce instanceof PrefetchCompleteEvent) {
            if (visual != null)
                return;

            if ((visual = mplayer.getVisualComponent()) != null) {
                Dimension size = visual.getPreferredSize();
                videoWidth = size.width;
                videoHeight = size.height;
                getContentPane().add("Center", visual);
            } else
                videoWidth = 320;
            if ((control = mplayer.getControlPanelComponent()) != null) {
                controlHeight = control.getPreferredSize().height;
                getContentPane().add("South", control);
            }
            setSize(videoWidth + insetWidth, videoHeight + controlHeight
                    + insetHeight);
            validate();
            mplayer.start();
        } else if (ce instanceof EndOfMediaEvent) {
            mplayer.setMediaTime(new Time(0));
            mplayer.start();
        }
    }
}

D:\hadoop>jar cvf db.jar *.class
             added manifest
          adding: JMFrame$1.class(in = 600) (out= 377)(deflated 37%)
        adding: JMFrame.class(in = 2362) (out= 1329)(deflated 43%) 
            adding: myapp$1.class(in = 469) (out= 334)(deflated 28%)
            adding: myapp$2.class(in = 1330) (out= 807)(deflated 39%)
          adding: myapp$3.class(in = 631) (out= 436)(deflated 30%)
      adding: myapp.class(in = 3283) (out= 1787)(deflated 45%)

D:\hadoop>java -jar db.jar
  no main manifest attribute, in db.jar

I can compile my file with no issues but when it comes to creation of jar file it notifies me error of java.io.FileNotFoundException jni error please check your installation and also about manife.st file.我可以毫无问题地编译我的文件,但是在创建 jar 文件时,它会通知我 java.io.FileNotFoundException jni 错误的错误,请检查您的安装以及关于 manife.st 文件。 Just correct my the segment of code where I made a mistake只需更正我犯错误的代码段

How are you attempting to build your JAR?你是如何尝试构建你的 JAR 的? The problem looks like this is trying to build an executable JAR which will run by calling the main method in the Main class (this is a common default), but since you don't have a Main class it can't do this.问题看起来像是试图构建一个可执行 JAR,该 JAR 将通过调用Main类中的main方法运行(这是一个常见的默认值),但由于您没有Main类,因此无法执行此操作。

I'd suggest either change this configuration to call your actual class, or relocate your main method to a class with this name.我建议要么更改此配置以调用您的实际类,要么将您的main方法重新定位到具有此名称的类。

(Posting as a separate answer because the edit to the question shows a totally different issue) (作为单独的答案发布,因为对问题的编辑显示了一个完全不同的问题)

The problem you're having running the JAR is that the JAR doesn't specify an entry point, so the jar command cannot execute it.您在运行 JAR 时遇到的问题是 JAR 未指定入口点,因此jar命令无法执行它。 Because you're not including a manifest when you create your JAR, jar is auto-generating an empty one for you.因为您在创建 JAR 时没有包含清单,所以jar会自动为您生成一个空的。

To fix this, change your JAR-building command to:要解决此问题,请将您的 JAR 构建命令更改为:

jar cvfe db.jar myapp *.class

Then you'll be able to execute it as you were.然后你就可以像以前一样执行它了。 (Note that myapp here is the fully-qualified class name. Since you haven't shown a package declaration, I've assumed you're using the default package. If not, make sure to include this here). (请注意,这里的myapp是完全限定的类名。由于您没有显示package声明,我假设您使用的是默认包。如果没有,请确保在此处包含它)。 You'll also find that if you unzip it and look inside META-INF/MANIFEST.MF there's now the line:您还会发现,如果您解压缩它并查看META-INF/MANIFEST.MF中的内容,现在有一行:

Main-Class: myapp

More on this here .更多关于这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.FileNotFoundException-找不到文件 - java.io.FileNotFoundException— File not found java.io.FileNotFoundException,找不到文件 - java.io.FileNotFoundException, file not being found 访问jar中的json文件时出现java.io.FileNotFoundException - java.io.FileNotFoundException when accessing json file in jar java.io.FileNotFoundException:即使文件存在于 src/main/resources 中,类路径资源 - java.io.FileNotFoundException: class path resource even though file exists in src/main/resources 没有这样的文件或目录 - 线程“主”java.io.FileNotFoundException 中的异常 - No such file or directory - Exception in thread “main” java.io.FileNotFoundException java.io.FileNotFoundException:找不到文件configuration.yml - java.io.FileNotFoundException: File configuration.yml not found Java FileWriter 类 - java.io.FileNotFoundException: * 没有这样的文件或目录 -Ubuntu - Java FileWriter class - java.io.FileNotFoundException: * no such file or directory -Ubuntu 线程“ main”中的异常java.io.FileNotFoundException: - Exception in thread “main” java.io.FileNotFoundException: java.io.FileNotFoundException:类路径资源 - java.io.FileNotFoundException: class path resource java.io.FileNotFoundException: - java.io.FileNotFoundException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM