Skip to content
Advertisement

How do I create a jar file via the command line?

Lets say I have these .java files:

  • MainClass.java
  • 2ndClass.java
  • BarClass.java
  • FooClass.java

Then I turn them into .class files:

  • MainClass.class
  • 2ndClass.class
  • BarClass.class
  • FooClass.class

How would I make it a jar file, and have MainClass.class be the first to be loaded?

Answer

Observing what Elliott said about class files, you can create (in recent versions of jar) the manifest implicitly with

jar cvfe m.jar MainClass *.class

That assumes that MainClass is in the default package (‘no package’) (not good practice). That will then run with

java -jar m.jar
Advertisement