Compile Java Code from Console

I have learn how to create java code last week. I am use Eclipse to create the code and interesting how to compile java code manually from console. We can get some of help how to compile java code manually by typing in console :

javac

Example, I have java code with name : example.java. We can compile this code to *.class with command :

javac example.java

We can define output example.class location with adding “-d” parameter. Example, I want to set location for the class output file at “bin” directory. So, modify the compile command with :

javac -d ./bin example.java

If our code not in the current active directory (example : source location in src directory), we can modify the command with :

javac -d ./bin -sourcepath ./src example.java

Our code use some of external library (*.jar”) in “lib” directory. So, modify the program with command :

javac -d ./bin -sourcepath ./src ./lib/external_library.jar example.java

The output from above command is *.class file. We can convert java class to jar with command :

jar cf example.jar example.class

We can get more explanation related with java command console parameter from javac command. Using the above example, we can compile our java code without depend with our favorite editor (compile java from console).

Add a Comment

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