Search code examples
javacentosjavaccentos8

Centos-8 JAVA code not executing program and JAVAC commands not working


I want to configure the server to run java and javac command. I have code that works into my local windows system but not into live Centos.

I have been given VM where I can check the JAVA version but the JAVAC command is not working. Also when I try to execute simple hello world java code it's not working. The following are some configuration details that I found. I have tried to locate the javac executable but I am unable to locate it.

CentOS Version

[root@server2 java]# rpm --query centos-release
centos-release-8.1-1.1911.0.9.el8.x86_64

Finding java package details

[root@server2 java]# find /usr -name java
/usr/share/java
/usr/bin/java
/usr/lib/java
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el8_1.x86_64/jre/bin/java

Finding Java version

[root@server2 /]# java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Finding Javac version ( Java compiler ) ( Also tested 'locate javac' )

[root@server2 /]# javac -version
bash: javac: command not found

Tried to run simple HelloWorld.java ( This works into local window system )

[root@server2 java]# java HelloWorld.java
Error: Could not find or load main class HelloWorld.java

Question/Suggestion

  1. How to know if JavaCompiler is already installed or not?
  2. If already installed how to find the correct path and set into the environment.

Any suggestions or solutions appreciated.


Solution

  • How to know if JavaCompiler is already installed or not?

    The message, bash: javac: command not found already tells you that the Java compiler (which is part of JDK) is missing.

    [root@server2 /]# javac -version
    bash: javac: command not found
    

    The problem is that you have installed only JRE and missed to install JDK. Please install JDK and the issue will be resolved.

    Apart from this, the following syntax works only from Java-11 onwards:

    java HelloWorld.java
    

    For a lower version, you need to execute the class file as shown below:

    java HelloWorld
    

    However, for this to be executed, HelloWorld.java must be compiled using javac tool which is a part of JDK.

    Note that starting with version-11, JRE does not exist anymore. Starting with Java-11, all you need to install is JDK.