简体   繁体   中英

Could not find main class

Okay, so I've been trying to make and executable jar file. It runs with the command "java -jar bybys.jar", but when i try to run it with enter it gives me an error "Could not find the main class bardejov.Image. Program will exit."

Here's the manifest :

Manifest-Version: 1.0
Created-By: 1.7.0_02 (Oracle Corporation)
Main-Class: bardejov.Image 

(yes i used a new line)

When compiling the jar file I tried every possible combination with the directory, I don't know where the problem is. I used - C:\\Java\\2D>jar cfm bybys.jar Manifest.txt bardejov/Image.class bardejov/Board.class bardejov/*jpg

The directory is:

META-INF/
META-INF/MANIFEST.MF
bardejov/Image.class
bardejov/Board.class
bardejov/siknius.jpg

And the main class:

package bardejov;

import javax.swing.JFrame;


public class Image extends JFrame {

public Image() {

    add(new Board());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(477, 530);
    setLocationRelativeTo(null);
    setTitle("Siknius");
    setVisible(true);
    setResizable(false);
}

public static void main(String[] args) {
    new Image();
}
}

How to fix?

UPDATE

I fixed it. The problem was I didin't have the newest JRE installed.

There's an extra space character after the name fof the class in your Manifest.txt. This is hinted by the error message:

Could not find the main class bardejov.Image .
                                            ^^

The problem actually appears to be that the jar doesn't contain an entry for the directory bardejov ; it contains only entries for the files in the directory. You can see this in your listing; see how there's an entry for bardejov . When you create the jar file, you have to tell jar to include the directory, not just the files in it:

jar cfm bybys.jar Manifest.txt bardejov

As from Java 6 you can specify an entry point with the jar command. The following command should create an executable jar file for your application:

jar cfe bybys.jar bardejov.Image bardejov/Image.class bardejov/Board.class bardejov/*jpg

You don't need to write and add a custom manifest.

Resource

Update

The following works on my machine:

create a java source file at example/Hello.java :

package example;
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

compile with command

javac example/Hello.java

jar with command

jar cfe example.jar example.Hello example/*.class

execute with command

java -jar example.jar

The output is

Hello world!

Update2

Now it looks like a configuration issue. The code and the jar are obviously correct.

For a quick fix/workaroud: Instead starting the jar directly, write a short batch or shell script file that simply executes the java -jar ... command.

open cmd prompt and type

set JAVA_HOME=c:\PATH\TO\JAVA_DIRECTORY
set CLASSPATH=.;%JAVA_HOME%\bin;%JAVA_HOME%\lib

make sure to include these variables in your PATH environment variable, as well.

You can get to environment variables in Win7 by going to Control Panel -> System -> Advanced System Settings -> Advanced tab -> Environment Variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM