Gr Cp
Gr Cp

Reputation: 11

What is the proper entry point, when compiling java to jar?

So, when I write my java file:

public class Program
{
public static void main(String[] args)
{
System.out.println("Serious business logic.");  
}
}

Then in windows cmd, I compile this way:

javac Program.java
jar cfe Program.jar Program Program.class
java -jar Program.jar

It's fine, and the result is:

"Serious business logic."

When in Netbeans I create a Project, it adds this line:

package program;

And I can not compile in cmd, only inside the IDE. I've tried manifest.txt, UTF8 encoding without BOM, plus linebreak at the and of file.

Manifest.txt:

Main-Class: program.Program  

and without manifest.txt, just in cmd program.Program

When I tried to run:

java -jar Program.jar

it results in:

"Error: Could not find or load main class program.Program"

I've already checked the following websites:
http://www.skylit.com/javamethods/faqs/createjar.html
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
and haven't got any idea how to do. Could you please help me?
How do I compile with package keyword? What is the proper entry point?

Thanks!

(ps jre1.8.0_91 ; jdk1.8.0_66 should I use same 32 or 64 bit for both of jre and jdk?)

Upvotes: 1

Views: 417

Answers (1)

Dylan Wheeler
Dylan Wheeler

Reputation: 7074

Make sure when you are compiling your program as a JAR, Program.class is inside of a folder called program. The package keyword that Netbeans has added at the beginning of your script is telling the executable that it is inside of a folder called program. If you are just adding the class file without making sure it is in the correct package (folder), it will not run properly because it does not know where to find it. Your command should be changed to:

jar cvfm Program.jar Manifest.txt program

where program is a folder containing Program.class. Your manifest may be left alone but also needs to be included with compilation.

Upvotes: 1

Related Questions