Lets assume you have following directory structure:
MyJavaProject
|-src
|- com
|- example
|- Main.java
To compile such project in cmd line, (no external dependencies) you need to invoke commands
$ cd MyJavaProject
$ mkdir bin //to separate *.class file from source files
$ javac -d bin src\com\example\Main.java
This will create Main.class
file in bin
directory. To pack it to *.jar file, you can:
1) create jar with binary files and specify Main class in cmd
2) create Manifes and embbed it to jar (I will focus on this one)
You should create META-INF
directory under src
and inside it create MANIFEST.mf
file
Your manifest should look like this:
Manifest-Version: 1.0
Created-By: <Your info>
Main-Class: com.example.Main
Remember to add empty line at end on Manifest!!
In this case, you specify Manifest-Version
attribute, Created-By
attribute, and fully qualified name of main class to run in Main-Class
attribute
To create Jar with this Manifest file and you binary file, invoke commands
$ cd bin
$ jar cfm MyJavaProject.jar ..\src\META-INF\MANIFEST.MF .
this will create new jar called MyJavaProject.jar
and use your manifest
If you project depends on external classas or jar, add them to your classpath when compiling (-cp
option) and add another line in Manifest
ClassPath: path/to/dependent/jars/jar.jar
Recompile it and create new jar and enjoy your Java quest :)
For more info on Manifest please see: docs
PS: Working with jars,amnifest from cmd line might seem ugly, but it can teach you some ava-like concepts. If you want to skip it, please consider using Apache Maven or Apache Ant