Skip to content
Advertisement

Compile java file in linux [closed]

I have added environment variables in /etc/profile and the compiled file has no package name. Why java Hello can not work and appear java.lang.ClassNotFoundException: HelloWorld. I would like to know where is the problem, thanks.

compile java file in linux Here is the code.

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello world on Linux.");
    }
}

Linux execute java command Error: Main class not found or loaded
java环境变量配置可参考
Java环境变量配置(CentOS)

以上回答转自博客园提问 谢谢 ycyzharry

Advertisement

Answer

You cannot run just java HelloWorld because in Linux, current directory “.” is not in $CLASSPATH. (You can observe by running command echo $CLASSPATH)

Because of that, when you are specifying class path with -cp .; you are basically saying “When running that class, look for that class path.”

Thus, you can run the command with second way.

For further information, you can check out Oracle’s documentation.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement