Compile and run Eclipse Java projects from Linux terminal

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/
java -cp “all jar file’s path”

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:

Second, run the project using the following command.

java -cp ".:/home/xiaoran/workspace/TerminalEclipse/lib/commons-io-2.4.jar:

*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){
			sb.append(f.getAbsolutePath() + ":");
	String s = sb.toString();
	s = s.substring(0, s.length()-1);
Category >> Basics >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Nachiket N

    I have two files in my package in Eclipse 1) Main class Stu_rec,java 2)
    I have also used an external jar file opencsv.jar. has “main ” in it and uses objects of
    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?