简体   繁体   English

在Linux上编译并运行HelloWorld.java

[英]Compile and Run HelloWorld.java on linux

I have created HelloWorld.java file in a linux folder. 我已经在linux文件夹中创建了HelloWorld.java文件。 I would like to compile and run it. 我想编译并运行它。 As I am doing it first time, and no such question posted here. 当我第一次这样做时,这里没有发布这样的问题。 Below is the content of Java File: 以下是Java文件的内容:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello...How are you");
    }
}

I see Java is installed on the server. 我看到服务器上已安装Java。 Below are commands and their output. 以下是命令及其输出。

rakeshth@ldnserver000590 DEV $ ls
HelloWorld.java
rakeshth@ldnserver000590 DEV $ java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
rakeshth@ldnserver000590 DEV $ javac HelloWorld.java
-bash: javac: command not found
rakeshth@ldnserver000590 DEV $ java HelloWorld.java
Error: Could not find or load main class HelloWorld.java

Please, let me know steps to compile and run this Java file. 请让我知道编译和运行此Java文件的步骤。

-bash: javac: command not found

It seems that your javac application is not in the PATH or it's missing. 看来您的javac应用程序不在PATH中,或者丢失了。

First you should try to figure out whether you have installed a JRE or a JDK in your system. 首先,您应该尝试确定您是在系统中安装了JRE还是JDK。 Java Runtime Environment (JRE) does not have the javac command . Java Runtime Environment(JRE)没有javac命令 You need to get familiar with the linux commands to find files and symbolic links. 您需要熟悉linux命令才能找到文件和符号链接。 You should look for javac which should be under the JDK_installation_folder/bin ex. 您应该在JDK_installation_folder / bin ex下寻找javac。 "/usr/local/java/jdk1.7.0_51/bin/javac" “ /usr/local/java/jdk1.7.0_51/bin/javac”

Example: 例:

> whereis java
java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/local/java /usr/share/java
> readlink -f /usr/bin/java 
/usr/local/java/jdk1.7.0_51/jre/bin/java
> ls /usr/local/java/jdk1.7.0_51/bin/javac
/usr/local/java/jdk1.7.0_51/bin/javac

Last line proves that javac is there . 最后一行证明javac在那。 If you JDK is not installed download it here . 如果未安装JDK,请在此处下载。 if it is you can compile your example like this example 如果是这样,您可以像下面的示例一样编译您的示例

> /usr/local/java/jdk1.7.0_51/bin/javac HelloWorld.java
> ls HelloWorld.class 
HelloWorld.class
> java HelloWorld
Hello...How are you

If you want to put your javac in the path you could create a symbolic link to it. 如果要将javac放在路径中,则可以创建一个指向它的符号链接。 Example: 例:

> sudo ln -s /usr/local/java/jdk1.7.0_51/bin/javac /usr/bin/

If you want to put it in the path check out this tutorial. 如果您想将其放在路径中,请查看本教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM