How to run java on linux: first install jdk; then copy the code from windows to linux; then execute "javac" to generate the ".class" file; finally run the java program.
#I recently came into contact with Linux and it feels very fresh. What I have done under Windows before is also thought of being implemented in a Linux environment. I happened to be compiling Java at hand, and I thought that since Java can run under DOS operation in Windows, can it also be compiled and executed under Linux terminal? Curiosity is the best teacher, and the rich resources on the Internet helped me quickly implement this operation.
The specific implementation process is as follows:
To compile and execute java code, you first need a system that supports java Programs, the so-called jdk, java development kit are used for this. Whether under Windows or Linux, jdk is necessary to compile and execute java code.
JDK shields programmers from the differences between the two systems, allowing Java to achieve "write once and compile everywhere". The JDK package can be downloaded from the official website, and different installation packages can be downloaded according to different systems. The installation method is slightly different depending on the version of Linux. I am using RedHat Linux (RHEL 6) system. Download the corresponding .rpm file and install it like other software:
rpm -ivh jdk_8u25.rpm
If you use other systems, such as Ubuntu, you can download the .tar.gz compressed package for installation. There is a lot of information online, so I won’t go into details here.
After installing jdk, we can copy the code from windows to linux. Here we do not directly copy the .java code to the destination directory, but first generate the jar package and then transfer it.
Here we use Eclipse to achieve this operation. To export the project source code into a jar package under eclipse, you can right-click the project->export->jar. Generate the required 1.jar, and then prepare some additional jar packages, such as the commonly used lucene and mysql-connecor jar packages (these packages can also be packaged into one package), and copy these packages together to the same Linux directory directory (or upload to the linux server).
In addition, the main class file of the java code (containing the main function, such as main.java) must also be copied to the same directory.
implement. First execute javac to generate a .class file. The format is as follows:
javac -cp /home/username/../1.jar:/home.username/../2.jar:/home.username/../3.jar main.java
In this way, the java program will run.
The above is the detailed content of How to run java under Linux?. For more information, please follow other related articles on the PHP Chinese website!