How to compile and run an Eclipse Java project from a Linux terminal
This article focuses on "how to compile and run an Eclipse Java project from a Linux terminal". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to compile and run an Eclipse Java project from a Linux terminal.
Command line used
Basically all you need is the following two commands. The-cp parameter entry is used to configure the classpath, which points to the class and jar files used. If you use multiple libraries, then each should be included in it to "." Separate.
Javac-cp "all jar file's path" package/target.java
Java-cp "all jar file's path" package.target
An Example Eclipse Project an example of an Eclipse project
Below, I create a new Java project in eclipse. The project contains a class Test in package1. Test uses third-party libraries 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
Second, run the project using the following command.
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 * * part of the path is., which points to the current path.
The compilation process produces .class files under the / src/directory folder. You may want to delete them when you use eclipse again.
Potential problem
If you are developing a large-scale project, you may use a lot of third-party libraries. You can use the following code to generate the 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)
}
At this point, I believe you have a deeper understanding of "how to compile and run an Eclipse Java project from a Linux terminal". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!