Sign in to follow this  
felicia

"Could not find or load main class . . ."

Recommended Posts

I have been using eclipse as my IDE for Java programming but I cannot figure out what is going on. I'm compiling a simply Hello World program but it won't run outside Eclipse. I have googled this problem for hours and tried many solutions but nothing has worked.

 

Code:

public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

Error:

10fyfm9.jpg

 

Works fine in Eclipse:

347gy6h.jpg

Share this post


Link to post
Share on other sites

Jar files require a manifest file, which must be located inside the file under /META-INF/MANIFEST.MF
 
The manifest file needs to contain specific information so the .jar can be run.  Many items on that link are optional, such as the methods for signing the jar, but a minimal jar would probably be:

Manifest-Version: 1.0
Class-Path: HelloWorld.jar
Main-Class: HelloWorld

Share this post


Link to post
Share on other sites

Jar files require a manifest file, which must be located inside the file under /META-INF/MANIFEST.MF
 
The manifest file needs to contain specific information so the .jar can be run.  Many items on that link are optional, such as the methods for signing the jar, but a minimal jar would probably be:

Manifest-Version: 1.0
Class-Path: HelloWorld.jar
Main-Class: HelloWorld

 

I've tried editing the Manifest file several times in different ways but it still gives me the same error. I extracted the contents of the .jar, edited the manifest file and used 7zip to open the .jar again to overwrite the manifest with the one with the changes I made. I even used the exact same text you gave me. I'm still getting the same error.

Share this post


Link to post
Share on other sites
java -jar HelloWorld.jar 

// Did you export with resources from Eclipse?

 

or

 

You could use a plain old text editor to create that program. Save the file with .java extension and execute the following:

javac HelloWorld.java  // compiles the source file
java HelloWorld  // runs program

Or use Netbeans. It automagically generates an executable .jar file in the dist folder.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this