How to compile and run Java Projects with java and javac commands

Compiling

-d is output directory for new generated class files.

How to compile single Java source file

javac -d ./target/classes src/main/java/com/extuni/enum_demo4/App.java

How to compile multiple Java source files

javac -d ./target/classes 
./src/main/java/com/extuni/enum_demo4/App.java 
./src/main/java/com/extuni/enum_demo4/Greeting.java 
./src/main/java/com/extuni/enum_demo4/Helper.java

How to compile multiple Java source files using wildcard

javac -d ./target/classes ./src/main/**/*.java

How to specify dependency jar file when compiling

Trying to compile ./src folder completely will throw exception due to junit.jar file dependency. So, it should be specified using -cp option which means –classpath as below.

javac -d ./target/classes -cp /Users/kenanhancer/.m2/repository/junit/junit/4.11/junit-4.11.jar ./src/**/*.java

or

javac -d ./target/classes --class-path  /Users/kenanhancer/.m2/repository/junit/junit/4.11/junit-4.11.jar ./src/**/*.java

or

javac -d ./target/classes -classpath  /Users/kenanhancer/.m2/repository/junit/junit/4.11/junit-4.11.jar ./src/**/*.java

Packaging – creating jar file

jar CLI some options are the following

-v, –verbose Generate verbose output on standard output.

-f, –file=FILE The archive file name.

-C DIR Change to the specified directory and include the following file

-x, –extract Extract named (or all) files from the archive

-t, –list List the table of contents for the archive

-c, –create Create the archive

-e, –main-class=CLASSNAME The application entry point for stand-alone applications bundled into a modular, or executable, jar archive

1- Package

jar cf ./target/enum_demo3-1.0-SNAPSHOT.jar -C ./target/classes .

or

jar cfv ./target/enum_demo3-1.0-SNAPSHOT.jar -C ./target/classes .

new generated enum_demo3-1.0-SNAPSHOT.jar file is added in ./target folder.

1.1- Package with new manifest file

If you see the following error in your terminal, it mean we have to specify Main-Class property in the manifest file when we are packaging.

While packaging, we should specify main entrance class. So, we should create Manifest.txt file with Main-Class property as the following and one empty line

jar cfm ./target/enum_demo3-1.0-SNAPSHOT.jar ./Manifest.txt -C ./target/classes .

or

jar cfe ./target/enum_demo3-1.0-SNAPSHOT.jar com.extuni.enum_demo3.App -C ./target/classes .

1.2- Updating existing jar with new manifest file

jar ufm ./target/enum_demo3-1.0-SNAPSHOT.jar Manifest.txt

2- Extracting

jar xf ./target/enum_demo3-1.0-SNAPSHOT.jar

or

jar xfv ./target/enum_demo3-1.0-SNAPSHOT.jar

2.1- Extracting only files under a specific package

jar xfv ./target/enum_demo3-1.0-SNAPSHOT.jar 

3- Viewing content of a jar file

jar tf ./target/enum_demo3-1.0-SNAPSHOT.jar

or

jar tfv ./target/enum_demo3-1.0-SNAPSHOT.jar

Running – the main() method

-cp or –class-path or -classpath is class path for Java application.

java -cp ./target/classes com.extuni.enum_demo4.App

or

java --class-path ./target/classes com.extuni.enum_demo4.App

or

java -classpath ./target/classes com.extuni.enum_demo4.App

How to specify other project's compiled class folder in -classpath when running

javac -d ./target/classes -cp ../enum_demo3/target/classes ./src/main/java/com/extuni/enum_demo4/App.java

How to specify other project's packaged jar file in -classpath when running

javac -d ./target/classes -cp ../enum_demo3/target/enum_demo3-1.0-SNAPSHOT.jar ./src/main/java/com/extuni/enum_demo4/App.java

Running – jar file

java -jar usage

if the jar file has a Main-Class defined in manifest file, the following command will run.

java -jar ./target/enum_demo3-1.0-SNAPSHOT.jar

java -cp usage

Execute another class with a public static void main method. So, -cp mean class path

java -cp ./target/enum_demo3-1.0-SNAPSHOT.jar com.extuni.enum_demo3.App

Sample Scenario – Using one project in other Java Project as a dependency

If we have one Java project which uses other Java project as a dependency, then we have to specify those dependencies in javac and java commands.

So I will have two projects named enum_demo3 and enum_demo4

enum_demo4 project will use Greeting class's sayHello() method in enum_demo3 project.

For example

Compiling enum_demo3 project

javac -d ./target/classes ./src/main/java/com/extuni/enum_demo3/*.java

or

javac -d ./target/classes ./src/main/*/.java

I want to use Greeting class written in enum_demo3 project above. So i used it as below.

Compiling enum_demo4 project

javac -d ./target/classes -cp ../enum_demo3/target/classes ./src/main/java/com/extuni/enum_demo4/App.java

or

javac -d ./target/classes -cp ../enum_demo3/target/classes ./src/main/**/*.java

or

javac -d ./target/classes -cp ../enum_demo3/target/enum_demo3-1.0-SNAPSHOT.jar ./src/main/**/*.java

Running main method

java -cp "./target/classes:../enum_demo3/target/classes" com.extuni.enum_demo4.App

or

java -cp "./target/classes:../enum_demo3/target/enum_demo3-1.0-SNAPSHOT.jar" com.extuni.enum_demo4.App

Leave a Reply

Your email address will not be published. Required fields are marked *