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

Started by
5 comments, last by TheChubu 7 years, 12 months ago

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

Advertisement

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

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.

Eh? I thought the proper way to launch jars was:

java -jar /path/to/jar

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.

Is your manifest located in the correct location inside the file? It needs to be /META-INF/MANIFEST.MF

Make sure to export from Eclipse as runnable jar, not as "regular" library jar.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement