杰瑞科技汇

Java文件在Linux下如何操作?

Of course! The phrase "java file linux" can mean several things. I'll cover the most common interpretations, from the basics of running a Java program on Linux to more advanced topics like compiling and managing Java projects.

Java文件在Linux下如何操作?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide.


Scenario 1: You have a .java file and want to run it on Linux.

This is the most common scenario. You need to do two things:

  1. Compile the .java source file into a .class bytecode file using the Java compiler (javac).
  2. Run the compiled bytecode using the Java Virtual Machine (java).

Let's assume you have a simple Java file named HelloWorld.java.

Step 1: Create the Java File

Open a terminal and create the file using a text editor like nano or vim.

Java文件在Linux下如何操作?-图2
(图片来源网络,侵删)
nano HelloWorld.java

Paste the following code into the file and save it (Ctrl+X, then Y, then Enter in nano).

// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Linux World!");
    }
}

Important: The filename must exactly match the public class name (including the capitalization). So, public class HelloWorld must be in a file named HelloWorld.java.

Step 2: Check if Java is Installed

Before you can compile or run anything, you need the Java Development Kit (JDK) installed.

Open your terminal and type:

Java文件在Linux下如何操作?-图3
(图片来源网络,侵删)
javac --version
  • If you see a version number (e.g., javac 17.0.8), you're good to go! You can skip to Step 3.
  • If you get an error like command not found: javac, you need to install Java. See the section below on Installing Java on Linux.

Step 3: Compile the Java File

Use the javac command to compile your source file. This creates a HelloWorld.class file in the same directory.

javac HelloWorld.java

You can now list the files in your directory to see the new .class file:

ls -l
# You should see:
# -rw-r--r-- 1 user user   123 Oct 26 10:30 HelloWorld.class
# -rw-r--r-- 1 user user   123 Oct 26 10:30 HelloWorld.java

Step 4: Run the Compiled Java Program

Use the java command to execute your program. Notice that you do not include the .class extension here.

java HelloWorld

You should see the output:

Hello, Linux World!

Scenario 2: You need to install Java on Linux.

If javac was not found, you need to install a Java Development Kit (JDK). The easiest way to do this on most modern Linux distributions is with your system's package manager.

For Debian / Ubuntu / Mint

Use apt. It's highly recommended to use a versioned package (e.g., openjdk-17-jdk) for stability.

# Update your package list
sudo apt update
# Install OpenJDK 17 (a popular, modern LTS version)
sudo apt install openjdk-17-jdk
# Verify the installation
javac --version
java --version

For Fedora / CentOS / RHEL

Use dnf (or yum on older systems).

# Install OpenJDK 17
sudo dnf install java-17-openjdk-devel
# Verify the installation
javac --version

For Arch Linux

Use pacman.

# Install the OpenJDK 17 package
sudo pacman -S jdk17-openjdk
# Verify the installation
javac --version

Scenario 3: You have a Java project (e.g., using Maven or Gradle).

Modern Java projects are rarely managed as single .java files. They use build tools like Maven or Gradle to handle dependencies, compilation, packaging, and running.

Let's assume you have a Maven project.

Project Structure

A typical Maven project looks like this:

my-java-project/
├── pom.xml          (Maven's configuration file)
└── src/
    └── main/
        └── java/
            └── com/
                └── mycompany/
                    └── App.java

How to Build and Run a Maven Project

  1. Navigate to the project's root directory (the one containing pom.xml).

    cd /path/to/my-java-project
  2. Compile the project. Maven will automatically download dependencies and compile all source files.

    mvn compile
  3. Run the main class. You can use the exec:java plugin to run your application directly from Maven.

    First, you might need to tell Maven which class to run. You can do this by adding a <mainClass> to your pom.xml or by specifying it on the command line.

    # Run the main class specified in the pom.xml
    mvn exec:java
    # Or specify the class directly on the command line
    mvn exec:java -Dexec.mainClass="com.mycompany.App"
  4. Package the project into a JAR file.

    mvn package

    This command creates a .jar file in the target/ directory (e.g., my-java-project-1.0-SNAPSHOT.jar).

  5. Run the packaged JAR file.

    java -jar target/my-java-project-1.0-SNAPSHOT.jar

Scenario 4: Advanced - Setting the JAVA_HOME Environment Variable

Some tools (like Tomcat, Gradle, or IDEs) require the JAVA_HOME environment variable to be set to the root directory of your JDK installation.

How to Find Your Java Home

Run this command in your terminal:

# For OpenJDK, this usually works
echo $JAVA_HOME
# If it's not set, find it with:
readlink -f $(which javac)
# Example output: /usr/lib/jvm/java-17-openjdk-amd64/bin/javac
# The path you want is one level up: /usr/lib/jvm/java-17-openjdk-amd64

How to Set JAVA_HOME (Temporarily for the current session)

This is useful for testing.

# Find your JDK path first, e.g., /usr/lib/jvm/java-17-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
# Verify it's set
echo $JAVA_HOME

How to Set JAVA_HOME (Permanently)

The method varies by shell and distribution. The most common file to edit is ~/.bashrc or ~/.profile.

  1. Open the file with a text editor:

    nano ~/.bashrc
  2. Add the following line to the end of the file (replace the path with your own):

    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
    export PATH=$JAVA_HOME/bin:$PATH

    Adding $JAVA_HOME/bin to the PATH ensures that commands like java and javac are always found.

  3. Save the file and exit (Ctrl+X, Y, Enter in nano).

  4. Apply the changes to your current terminal session by "sourcing" the file:

    source ~/.bashrc
  5. Verify the change is permanent:

    echo $JAVA_HOME

Summary

Task Command(s) Notes
Compile a single file javac MyProgram.java Creates MyProgram.class. Requires the JDK to be installed.
Run a compiled class java MyProgram Note: No .class extension.
Install Java (Debian) sudo apt install openjdk-17-jdk Use your system's package manager.
Run a Maven project mvn compile then mvn exec:java Navigate to the directory containing pom.xml.
Run a packaged JAR java -jar my-app.jar The JAR must be an "executable" JAR (with a Main-Class manifest).
Set JAVA_HOME export JAVA_HOME=/path/to/jdk Set permanently in ~/.bashrc or similar shell configuration file.
分享:
扫描分享到社交APP
上一篇
下一篇