6

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???

JREN
  • 3,572
  • 3
  • 27
  • 45
Pranav Chugh
  • 73
  • 2
  • 2
  • 4

6 Answers6

9

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.

Community
  • 1
  • 1
Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • It works with this... Why do i have to write -cp? What does it mean? – Pranav Chugh Jul 05 '13 at 06:52
  • Is this a requirement for linux as it works just find on Windows? – MadProgrammer Jul 05 '13 at 06:53
  • cp stands for ClassPath, which are all the paths/folders where classes can be found. the dot operator means "local directory" – Cristian Meneses Jul 05 '13 at 06:53
  • -cp parameter tells the runtime to search for class files in the given directories, which in your case is the present directory. Hence the argument -cp = ClassPath – Narendra Pathai Jul 05 '13 at 06:54
  • Check for a CLASSPATH environnemnt variable with `export`. – Thorbjørn Ravn Andersen Jul 05 '13 at 06:55
  • But i never used that before in ubuntu. is there a way to set it to use the default path which is the current directory? Any idea why it runs with the sudo command – Pranav Chugh Jul 05 '13 at 06:57
  • Did you update your java recently? Sometimes changes (especially security fixes) are not backward compatible. – Burkhard Jul 05 '13 at 07:17
  • @Burkhard - If he is running a Java application from the command line, his code is not sandboxed. It is extremely unlikely that a Java security fix will break things in that case. (Applets and Webstart applications are a different matter ... but irrelevant to the OP's use-case.) – Stephen C Jul 05 '13 at 13:03
  • Any idea why I don't have to add the -cp option? I can just `java classname` and it works for me. I also use fedora and I don't have to add the -cp option. – cokedude Oct 26 '13 at 23:36
  • This is a requirement on Windows, as well. Thanks, @Burkhard – marklark Feb 03 '16 at 18:09
4

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

SAR
  • 1,765
  • 3
  • 18
  • 42
0

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!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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

Steven
  • 1
0

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

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!

KidOfLaMancha
  • 51
  • 1
  • 2