Unable to start java prorgram with "java" command

I have compiled java codes with

razrlele@OVO:~/workspace/javastudy/src$ javac Helloworld.java 

      

and no errors or warnings are received

then i run the program with

razrlele@OVO:~/workspace/javastudy/src$ java Helloworld

      

he returns it

Error: Could not find or load main class Helloworld

      

I need to enter this

razrlele@OVO:~/workspace/javastudy/src$ java -cp ./ Helloworld

      

so that the program runs correctly.

I am confused as to why the "java" command is not working.

Here is my / etc / environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_11"                    
CLASSPATH=/usr/lib/jvm/jdk1.8.0_11                                                                                      
JAVA_HOME=/usr/lib/jvm/jdk1.8.0_11                                                                                      

      

+3


source to share


2 answers


Java will try to use your classpath to find class files. Since your classpath is set to /usr/lib/jvm/jdk1.8.0_11

, that's where it looks like.

By overriding the classpath with using -cp ./

, you tell it to look in the current directory for its class files.

There are several ways to fix this, including changing the classpath environment variable to include .

or other localized paths as needed. I prefer to just set up an alias (like in mine .bashrc

) so normal Java programs are not affected, something like (from memory):

alias jhere="java -cp $CLASSPATH:."

      



then I can just use:

jhere HelloWorld

      

to check my fragments.

+5


source


Because it is .

not in your CLASSPATH

, at first

export CLASSPATH="/usr/lib/jvm/jdk1.8.0_11:."

      

then



javac Helloworld.java
java Helloworld 

      

It is also possible to use -cp

(which is less for -classpath

) with java

on the command line, if you run java -h

you will see

-cp <class search path of directories and zip/jar files>
   -classpath <class search path of directories and zip/jar files>
              A : separated list of directories, JAR archives,
              and ZIP archives to search for class files.

      

+3


source







All Articles