The internet is bloated with tutorials that explain how to compile and run only a single damn Java source code. There were almost none tutorials that show how to do something "more sophisticated". Sure, you can use Eclipse IDE to do all the work for you, but what if you want to compile and run from command line? Here is a quick tutorial that will hopefully help you and end your search for something advanced. :)
Imagine that you have multiple Java source files that are part of some package. You want to compile then in a manner that you can place resulting compiled class files to folder other then your source files are located. On the top of that, you want to be able to run compiled files from any location (folder) on your computer, so you don't need to position yourself to classpath folder every time.
For this tutorial you will need to have Java Development Kit (JDK) installed and configured in your PATH environment variable in Linux or Windows. Of course, this tutorial works on both Linux and MS Windows.
1. Copy following Java source files and save then on harddisk. Program is very simple so I won't go into code details.
// IOperation.java package com.glamhacker.javacompile; public interface IOperation { public int doOperation(int a, int b); public String getOperationString(); }
// AddOperation.java package com.glamhacker.javacompile; public class AddOperation implements IOperation { public int doOperation(int a, int b) { return a + b; } public String getOperationString() { return "addition"; } }
// MultiplicationOperation.java package com.glamhacker.javacompile; public class MultiplicationOperation implements IOperation { public int doOperation(int a, int b) { return a * b; } public String getOperationString() { return "multiply"; } }
// SubOperation.java package com.glamhacker.javacompile; public class SubOperation implements IOperation { public int doOperation(int a, int b) { return a - b; } public String getOperationString() { return "subtraction"; } }
// OperationTest.java (main method is here) package com.glamhacker.javacompile; import java.util.ArrayList; import java.util.List; public class OperationTest { private Listoperations = new ArrayList (); public List getOperations() { return operations; } public static void main(String[] args) { OperationTest oTest = new OperationTest(); oTest.getOperations().add(new AddOperation()); oTest.getOperations().add(new MultiplicationOperation()); oTest.getOperations().add(new SubOperation()); for (int i=0; i < oTest.getOperations().size(); i++) System.out.println("5 " + oTest.getOperations().get(i).getOperationString() + " 10 is " + oTest.getOperations().get(i).doOperation(5, 10)); } }
2. Create following folder structure on your system and place entered Java source files in it. Make sure you have read and write permissions in those folders.
You can name folder JavaTests as you like. In this folder we place bin and src folders where compiled class files and source files are held respectively. All source files are part of com.glamhacker.javacompile package (defined in first package line for every source file) and you will need to recreate folder structure in src folder to follow package notation. Just create folders nested and separated by 'dot'. Now you have your source code files placed in given package. Keep in mind that if you place files in different folder structure (e.g. glam/java/com), compilation will fail! You must respect package structure or define other package.
3. Go to JavaTests folder and type execute selected line in command line to compile source files (terminal window in Linux or command line in Windows):
javac -cp src -d bin src/com/glamhacker/javacompile/OperationTest.java
javac is Java Compiler Program that ships with JDK and it is used for compiling java source files.
-cp src flag defines full path to classpath where to search for packages and other source files. Google about classpath to find more, since that term has multiple usages in Java. If you were in some other folder you will need to type absolute or relative path to src folder.
-d bin flag defines compiled class files output folder. Again, if you were in somewhere else folder you need to type absolute or relative path to bin folder (ahhh you figured it out don't you :))
src/com/glamhacker/javacompile/OperationTest.java is location to source file that contains main method. You need to specify source file with main method since to signal compiler to auto compile remaining source files. Compiling only IOperation.java, for example, will not compile other source files since they are not needed.
Result of running compiler will compile all required source files to class files and automatically create package structure to place compiled files.
Those Java class files are actual program that you need to run.
4. Execute following command to run compiled files. You still need to be in JavaTests folder.
java -cp bin com.glamhacker.javacompile.OperationTest
java is actual Just-In-Time Java compiler that actually run Java programs. Technically, this is compiling Java objects to your native machine code, but some other time about that :) You must have java.exe to run any Java program and it is part of JRE and JDK.
-cp bin flag is classpath where compiled classes are placed.
com.glamhacker.javacompile.OperationTest is actual "name" of class that contains main method that is executed when program starts. You can see that package name must be present.
After executing this, Java program runs and you can see its output in terminal. Following screen contains compilation, running and output of Java program:
Compiling, running and output of Java program |
Yup, this concludes this tutorial. It would be more practical to package compiled files in executable JAR file, but this will suffice for now :)
nomadsoulx@reborn - like :)
ReplyDeleteThanks for noticing that :D
ReplyDelete