-4

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?

TheTank20
  • 89
  • 1
  • 6
  • 6
    You would not. You would first compile your .java files to .class files. You would then create a [manifest file](https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html). Finally you would jar the class files and specify the manifest file. Documentation is at the link provided. – Elliott Frisch Jul 07 '21 at 23:41
  • I would suggest you to check Maven or Gradle, that will help you like nothing else – Alberto Sinigaglia Jul 07 '21 at 23:42
  • 1
    Please, check this [answer](https://stackoverflow.com/a/10132818/8370915). It might be helpful for you. – invzbl3 Jul 07 '21 at 23:55

1 Answers1

3

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
g00se
  • 3,207
  • 2
  • 5
  • 9