Skip to content

Recap on Java Compilation and Execution 👷🏼‍♂️👷🏼‍♀️

(2019-12-02)

Creating a jar file

jar cf myjar.jar HelloWorld.class

-cf - create file

Running jar file

java -cp myjar.jar HelloWorld

Note: need to add the jar to the classpath with -cp myjar.jar

Working with 3rd party library

Compiling with 3rd party lib

javac -cp ./lib/* HelloWorld.java

Assuming the 3rd party library is in the lib folder.

Running with a 3rd party lib

java -cp ./lib/*;./ HelloWorld

Note the separator in the classpath is either ; (on windows) or : (on Mac/Linux)

File Structure

src
 |
 +-- main
      |
      +-- java
           |
           +--> source files go here!