How to run java under Linux?

May 18, 2019 pm 04:39 PM
java linux

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.

How to run java under Linux?

#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:

  • Installing JDK

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.

  • Generate JAR file

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.

  • Compile and execute

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Edit bookmarks in chrome Edit bookmarks in chrome Aug 27, 2025 am 12:03 AM

Chrome bookmark editing is simple and practical. Users can enter the bookmark manager through the shortcut keys Ctrl Shift O (Windows) or Cmd Shift O (Mac), or enter through the browser menu; 1. When editing a single bookmark, right-click to select "Edit", modify the title or URL and click "Finish" to save; 2. When organizing bookmarks in batches, you can hold Ctrl (or Cmd) to multiple-choice bookmarks in the bookmark manager, right-click to select "Move to" or "Copy to" the target folder; 3. When exporting and importing bookmarks, click the "Solve" button to select "Export Bookmark" to save as HTML file, and then restore it through the "Import Bookmark" function if necessary.

Enter key not working on my keyboard Enter key not working on my keyboard Aug 30, 2025 am 08:36 AM

First,checkforphysicalissueslikedebrisordamageandcleanthekeyboardortestwithanexternalone;2.TesttheEnterkeyindifferentappstodetermineiftheissueissoftware-specific;3.Restartyourcomputertoresolvetemporaryglitches;4.DisableStickyKeys,FilterKeys,orToggleK

What is a memory leak in Java? What is a memory leak in Java? Aug 28, 2025 am 05:37 AM

AmemoryleakinJavaoccurswhenunreachableobjectsarenotgarbagecollectedduetolingeringreferences,leadingtoexcessivememoryusageandpotentialOutOfMemoryError.Commoncausesincludestaticcollectionsretainingobjectsindefinitely,unclosedresourceslikestreamsorconne

How to find the max or min value in a Stream in Java How to find the max or min value in a Stream in Java Aug 27, 2025 am 04:14 AM

Use the max() and min() methods to combine Comparator to find the maximum and minimum values ​​in the stream, such as Comparator.naturalOrder() or Integer::compareTo compare basic types; 2. For custom objects, use Comparator.comparing() to compare based on specific fields, such as Person::getAge; 3. Since the result is Optional, the empty stream situation must be handled. You can use isPresent() to check or orElse() to provide default values. It is recommended to use IntStream for basic types to avoid boxing overhead and improve performance. In the end, you should always be properly done.

The Ultimate Guide to Gaming on Linux with Steam and Proton The Ultimate Guide to Gaming on Linux with Steam and Proton Aug 29, 2025 am 09:41 AM

Yes,youcannowgameonLinuxeffectivelyusingSteamandProton.1)Proton,builtonWineandenhancedwithDXVKandVKD3D-Proton,enablesWindowsgamestorunonLinuxwithnear-nativeperformance.2)InstallSteamviayourdistro’spackagemanager,enableSteamPlayinsettings,andselectaPr

Solving Common Java NullPointerException Issues with Optional Solving Common Java NullPointerException Issues with Optional Aug 31, 2025 am 07:11 AM

Optional is a container class introduced by Java 8. It is used to clearly indicate that a value may be empty, thereby avoiding NullPointerException; 2. It simplifies nested null checking by providing map, orElse and other methods, preventing methods from returning null and standardizing collection return values; 3. Best practices include only returning values, avoiding the use of fields or parameters, distinguishing orElse from orElseGet, and not calling get() directly; 4. Optional should not be abused. If non-empty methods do not need to be wrapped, unnecessary Optional operations should be avoided in the stream; correct use of Optional can significantly improve code security and readability, but it requires good programming habits.

How to format numbers in Java using DecimalFormat How to format numbers in Java using DecimalFormat Aug 30, 2025 am 03:09 AM

Use DecimalFormat to accurately control digital formats. 1. Use pattern strings such as "#,###.##" for basic formatting, where # represents an optional number, 0 represents a must-display number, is a thousand separator, and is a decimal point; 2. Common modes include "0.00" to retain two decimal places, "0,000.000" to complement zero alignment, etc.; 3. Avoid scientific notation methods, you can setScientificNotation(false) or use a mode with sufficient digits; 4. You can set rounding mode through setRoundingMode(), such as HALF_UP, DOWN, etc.

Fixed: Windows Is Not Showing Recent Files in Quick Access Fixed: Windows Is Not Showing Recent Files in Quick Access Aug 29, 2025 am 09:06 AM

First enable the option "Show recently used files in Quick Access" to open the privacy settings in the File Explorer options and check the corresponding item; 2. If it is invalid, clear and rebuild the Quick Access cache, restart Windows Explorer by renaming the QuickAccess-related files in the Explorer folder; 3. Check the group policy or registry settings to ensure that NoRecentDocsHistory is not disabled and that the ShowRecentDocs value is 1; 4. Make sure that the activity history is turned on, go to the Privacy Settings to enable "Storing Activity History on Device", and confirm that the file operation is tracked normally, and finally restart the Explorer or computer to make the changes take effect. Usually this problem can be enabled by

See all articles