Hong
Hong

Reputation: 151

How to make .jar from .class

I have a working .class file but when I use

jar cvf MyJar.jar *.class

I the resulting MyJar.jar could not be opened:

"The Java JAR file “MyJar.jar” could not be launched. Check the CONsole for possible error messages."

Thanks for helping.

========== EDIT:

> build
  > MyClass.class
  > MyClassCanvas.class
  > MyClassFrame.class
  > manifest.txt

manifest.txt:

Main-Class: MyClass

Upvotes: 2

Views: 1024

Answers (1)

Lukino
Lukino

Reputation: 1455

As mentioned in comment, you need to add manifest to your jar. Call it manifest.txt and must have at least following line:

Main-Class: YourClass
# remove comment, but leave empty line

YourClass is same name as your YourClass.class file without extension and that class must have main method (public static void main(String[] args).

If you have you main class in subdirectories, you must use them (they are considered as packages. For instance if you have directory com/foo/YourClass.class

Main-Class: com.foo.YourClass
# remove comment, but leave empty line

Then pack it with your JAR:

jar cvfm MyJarName.jar manifest.txt *.class

For more see Manifest

EDIT:

I recommend to use at least maven. Make life easier if you have other dependencies or so. Then you add and configure maven-jar-plugin and that does a lot of stuff for you.

EDIT:

Alternatively, you don't need to use manifest file, but you can achieve all this with following command

jar cvfe MyJarName.jar MyClass *.class

where e - specify entry point - MyClass in your case

Upvotes: 1

Related Questions