Oh yes, the infamous class path and classdef exceptions – drive me nuts! So I thought it would be nice to have a quick post on the commands you can use to compile your Java files, package them into a jar and then run them. So let’s cut to the chase:
Step 1: Go to the directory above where your java files are located, and run this command:
javac -cp . foldername/*.java
This basically tells the JVM to compile all Java files and output the necessary class files files which end up being packaged in the JAR. Javac goes into the foldername folder and begins the compile.
Step 2: Package all the class files and output them to output.jar. The -cfe enables you to specify the main class of your program.
jar -cfe output.jarfoldername.ProjectMainClassfoldername/*.class
Step 3:Finally, run the jar file and specify external libraries such as jdbc, java mail etc (separate libraries with a colon 🙂
java -cp output.jar:jdbc.jarfoldername/ProjectMainClass
Please note that the colour coding is just to improve legibility.
I hope this helped!