Weird error on executing Java programs

Today, I had a weird error on executing a simple Java program (I have to confess that I didn’t use Java for some time ago :D). Suppose you got a HelloWorld.java file in which you just print the “Hello World!” message; after compiling that successfully, you want to execute it, but the compiler cry telling you that she can’t find or load the main class.

> java HelloWorld
> Error: no se ha encontrado o cargado la clase principal HelloWorld

What a weird error!!

First, I suppose that the class I have defined wasn’t Public, so I changed it as a Public Class, however it doesn’t work.

Then, after pondering it for a while, I thought of some path problem. I review carefully the path of the compiler, it was ok. Anyway I went to the root of the Java bin directory and recompile and rerun it again. mmmmm The problem stayed there. Then I said again “What a weird error”. :/

I don’t remember having these problems when I was learning Java. The only thing I remember was to avoid the .class extension of the compiled java file in order to execute it with the java command.

Finally the light came to me and I said: If she can’t find it, maybe she is finding it in the wrong place, so let’s give her a little more information of the environment. Then, I searched for some console arguments for setting the environment and I found the java -cp argument (ClassPath):

> java -cp . HelloWorld

And it worked.

In conclusion, personally I think that it was a weird error because I don’t remember having these problems before. And mainly and mostly because I think that the home directory of the file that someone is trying to execute must be the default directory of the classpath. So we don’t have to set -cp . argument.
Well, I don’t know if this error happened only to me, not sure whether it was the same before or since when it changed.

Advertisements

About gcarlos

Some people call me engineer, other developer or programmer, but I rather like be called scientist because that is what I really want to do ...
This entry was posted in Compiler, Java and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s