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

时间:2015-09-19 14:21:22

标签: java jar jmf

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

我可以编译我的文件没有任何问题,但是当涉及到创建jar文件时,它通知我java.io.FileNotFoundException错误jni错误请检查您的安装以及manife.st文件。只要纠正我犯了错误的代码段

2 个答案:

答案 0 :(得分:0)

您是如何尝试构建JAR的?问题似乎是尝试构建一个可执行的JAR,它将通过调用main类中的Main方法运行(这是一个常见的默认值),但由于你没有{{{ 1}}类不能这样做。

我建议您更改此配置以调用实际的类,或者将Main方法重定位到具有此名称的类。

答案 1 :(得分:0)

(作为单独的答案发布,因为对问题的编辑显示完全不同的问题)

您运行JAR的问题是JAR没有指定入口点,因此jar命令无法执行它。因为您在创建JAR时未包含清单,jar会自动为您生成一个清单。

要解决此问题,请将您的JAR构建命令更改为:

jar cvfe db.jar myapp *.class

然后你就能像现在一样执行它。 (注意myapp这里是完全限定的类名。由于你没有显示package声明,我假设你正在使用默认包。如果没有,请确保包括这里)。您还会发现,如果您解压缩它并查看META-INF/MANIFEST.MF内部现在就有了这条线:

Main-Class: myapp

有关此here的详情。