6
votes

Im using fedora 19. Content of HelloWorld.java :

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

I can successfully compile it using

javac HelloWorld.java

But i cannot run it using

java HelloWorld

It gives the following error

Error: Could not find or load main class HelloWorld

But i can run it using

sudo java HelloWorld

What am I missing here???

6
Do you have execute permission for java under your user?MadProgrammer
Did you accidently compile as the super user?PP.
@MadProgrammer How can i check that?Pranav Chugh
Maybe you can read this ? It look similar to your problem. stackoverflow.com/questions/12096016/java-cant-find-main-classLeep
@PP I have already given that a try. It still wont work.Pranav Chugh

6 Answers

9
votes

You are not setting a classpath that includes your compiled class! java can't find any classes if you don't tell it where to look.

Try java -cp . HelloWorld

Source here

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

4
votes

Dear Pranav Chugh,

1- cmd - go the directory of located java file

run the following on cmd

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

here you will get the result

0
votes

This is rather bizarre. It seems like the problem is that when you run java as a non-privileged user it cannot find or read the ".class" file. But when run as "root", you can.

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

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

Here are a couple of other possible explanations:

  • The java command you are running might not be what you think it is. Check what which java says when you run it as yourself. Check that it is the "real" java executable and not some script or something in the current directory or some other directory that won't be on the root / sudo $PATH.

  • You might have set your CLASSPATH environment variable such that the current directory (where "HelloWorld.class" is ... I assume) is not on the classpath. But when you sudo java, the java command is running with an environment in which $CLASSPATH is not set. In that case, if there is no -cp argument, you will get a default classpath consisting of just "."; i.e. the current directory.


If the problem turns out to be the CLASSPATH environment variable, I recommend that you unset it ... and edit your shell's "rc" files to unset it there too.

Instead, use the '-cp' command on the java command, javac command and so on ... and switch to either Ant or Maven or an IDE for building and running code. (Or you could write some little wrapper scripts as application launchers.)

Don't depends on the CLASSPATH environment variable. It is liable to give you nasty surprises, especially if you are switching between coding projects. (Certainly don't depend on it in your production environment!)

0
votes

I has the same issue just trying to run HelloWorld on Mac 10.7.5. I compiled the HelloWorld.java file without issue with javac. I then tried to run "java HelloWorld" and got the same error: "Could not find or load main class"

It was only after I changed the directory (cd) in the Mac Terminal to the directory containing the .class file that I was able to run the program.

HTH, Steve

0
votes

I have the same Problem before. Maybe you made the same mistake. My mistake was using "cd" to go inside the package directory rather than the directory right above it. For example wenn the directory right above are called "Hello", you can run it by typing: java Hello/HelloWorld

0
votes

It seems that your CLASSPATH setting is wrong. check your CLASSPATH and make sure that is :

CLASSPATH="YourJavaHome/lib:."

be attention there is a ':.' in the end of the sentence! after that run

source /etc/environment

and it should be work!