How to compile Java programs by using command line

So, do you need to compile your Java code using the command line? Don’t worry, just follow type command and it will be very easy!

java

// If we are in the root of the application
javac -d ./bin -classpath /Applications/eclipse/servers/apache-tomcat-7.0.50/lib/servlet-api.jar src/com/base22/servlets/*.java
  • -d: destination directory. In our example, the packages with bytecode will be generated into ./bin directory.
  • -classpath (-cp) : do we need any other library? In this case, we are adding servlet-api.jar library.
  • Last parameter indicates where are the *.java files.

Then, if we need to create a JAR file, we can follow these instructions: How to create a JAR file by using command line.

One comment on “How to compile Java programs by using command line”

Leave a Reply

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