If you are developing a Java project in Eclipse under a linux system, you may want to remote access the project from another location. You can remote desktop the linux box by using teamviewer, but sometimes that can be very slow. You can also edit, compile and execute your Java project from a regular ssh terminal. Using terminal to edit, compile and run your remote eclipse project is often faster. This post shows you how to compile and run eclipse project in terminal.
Command Lines Used
Basically, you need the following two command. The -cp option configures the class path, which points to the class files and the jar files used. If multiple libraries are used, each of them should be included and separated by “:”.
javac -cp “all jar file’s path†package/target.java java -cp “all jar file’s path†package.target
An Example Eclipse Project
In the following, I created a Java project under eclipse. The project contains one class Test
which is in package1
. The Test
class uses some third-party libraries under /lib/.
First, cd to /src/ directory, and compile the project using the following command.
javac -cp "/home/pc/workspace/TerminalEclipse/lib/commons-io-2.4.jar: /home/pc/workspace/TerminalEclipse/lib/commons-lang-2.5.jar" package1/Test.java
Second, run the project using the following command.
java -cp ".:/path/to/commons-io-2.4.jar: /path/to/lib/commons-lang-2.5.jar" package1.Test
*Note: the first part of path is .
which points to the current path.
The compilation process will generate .class files under /src/ directory. You may want to remove that when you are using eclipse again.
Potential Problems
If you are developing a large project, you may use a lot of third-party libraries. You can use the following code to generate the path strings.
public static void printAllJars(){ String str = "/home/pc/workspace/TerminalEclipse/lib"; File file = new File(str); StringBuilder sb = new StringBuilder(); File[] arr = file.listFiles(); for(File f: arr){ if(f.getName().endsWith(".jar")){ sb.append(f.getAbsolutePath() + ":"); } } String s = sb.toString(); s = s.substring(0, s.length()-1); System.out.println(s); } |
Thanks for this clear explanation, I could not find this before and always had to build my project in Netbeans before I could run it from a command line (in linux or windows).
I have a project developed in Netbeans with source package folder = src, but also other folders whith *.jar files needed to run the project. In Netbeans the 3 source package folders are listed in Properties/Sources/Source package folders like this :
D:ptwsamples
D:ptwmySamplesid
src
(I’m not using Eclipse but I guess these folders (D:ptwetcetera) would be listed in buildpath/configure buildpath/Projects)
How can I tell the compiler to also look in these folders for *.jar files ?
I have two files in my package in Eclipse 1) Main class Stu_rec,java 2) student.java
I have also used an external jar file opencsv.jar.
Stu_rec.java has “main ” in it and uses objects of student.java
It runs flawlessly in Eclipse but not in terminal
How do I compile them in terminal as it gives error stating that student object is not recognized?