Compile and run the Eclipse Java project from the Linux terminal

Source: Internet
Author: User

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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.