Compile and run the Eclipse Java project from the Linux terminal
If you use Eclipse to develop a Java project in linux, you may need to remotely connect to the project from other places. Maybe you can use teamviewer to remotely connect to the linux desktop, but it is often very fast. You can also modify, compile, and run your Java project from a conventional ssh terminal. Using the terminal to modify, compile, and run remote eclipse projects is much faster. This article describes how to compile and run an eclipse project on a terminal.
Command Line Used
Basically, you only need the following two commands. -The cp parameter item is used to configure the class path. It points to the class file and jar file used. If multiple databases are used, each database should be included and separated.
Javac-cp "all jar file's path" package/target. java
Java-cp "all jar file's path" package.tar get
An Example of An Eclipse Project
Next, I will create a new Java project in eclipse. This project contains the Test class in package1. Test uses a third-party library under/lib.
First, cd to the/src/directory folder, and then use the following command to compile the project.
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
2. Run the following command to run the project.
Java-cp ".:/home/xiaoran/workspace/TerminalEclipse/lib/commons-io-2.4.jar:
/Home/xiaoran/workspace/TerminalEclipse/lib/commons-lang-2.5.jar"
Package1.Test
Note: The first part of the path is.
, Pointing to the current path.
The. class file is generated in the/src/directory folder during compilation. When you use eclipse again, you may want to delete it.
Potential problems
If you are developing a large project, you may use many third-party libraries. You can use the following code to generate a path string.
Publicstaticvoid printAllJars (){
String str = "/home/pc/workspace/TerminalEclipse/lib ";
File = newFile (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 );
}