Java Could not find or load the main class

Im using fedora 19. Content HelloWorld.java:

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

I can compile it with

javac HelloWorld.java

But I can not start it with

java HelloWorld

It gives the following error:

Error: Could not find or load the main HelloWorld class

But I can run it using

sudo java HelloWorld

What am I missing here ???

+6
source share
6 answers

You do not set the class path that your compiled class includes! java cannot find any classes unless you tell where to look.

Try java -cp . HelloWorld java -cp . HelloWorld

Source here

Not sure why it works with sudo. My * would assume that CLASSPATH is set for the root user, and not for the current user.

+9
source

Dear Pranav Chew,

 1- cmd - go the directory of located java file 

run the following command on cmd

 2- javac HelloWorld.java 3- java HelloWorld ---- not not add .class 

here you get the result

+4
source

This is pretty weird. It seems that the problem is that when starting java as an unprotected user, he cannot find or read the ".class" file. But when running as "root" you can.

This suggests that you somehow managed to create the HelloWorld.class file with the wrong owner and / or wrong permissions.

Check permissions by running ls -l HelloWorld.class . The owner must be your user account (not "root"), and you need to get user permission on the file.

Here are a few other possible explanations:

  • The java command you run may not be what you think. Check what which java says when you run it as your own. Make sure that it is a β€œreal” java executable, and not some script or something in the current directory or another directory that will not be in the root directory / sudo $PATH .

  • You may have set the CLASSPATH environment variable so that the current directory (where "HelloWorld.class" ... I suppose) is not in the classpath. But when you sudo java , the java command works with an environment in which $CLASSPATH not set. In this case, if there is no -cp argument, you will get a default path consisting only of "." ; i.e. the current directory.


If the problem turns out to be a CLASSPATH environment variable, I recommend that you cancel it ... and edit the "rc" shell files to disable it too.

Instead, use the β€œ-cp” command in the java command, the javac command, etc .... and switch to Ant or Maven or the IDE to create and run the code. (Or you could write some small shell scripts as launcher applications.)

Independent of the CLASSPATH environment variable. It can give you unpleasant surprises, especially if you switch between coding projects. (Of course, do not depend on this in your production environment!)

0
source

I have the same problem, just trying to run HelloWorld on Mac 10.7.5. I compiled HelloWorld.java file without javac problems. Then I tried to run "java HelloWorld" and got the same error: "Could not find or load the main class"

Only after I changed the directory (cd) in the Mac terminal to the directory containing the .class file that I was able to run.

NTN, Steve

0
source

I have the same problem before. You may have made the same mistake. My mistake was to use "cd" to enter the package directory, not the directory directly above it. For example, if the directory above is called "Hello", you can start it by typing: java Hello / HelloWorld

0
source

It seems your CLASSPATH setting is incorrect. check your CLASSPATH and make sure it is:

 CLASSPATH="YourJavaHome/lib:." 

Please note: there is :. at the end of the sentence! after that do

 source /etc/environment 

and it should be!

0
source

Source: https://habr.com/ru/post/948762/


All Articles