I have been using Windows XP & the latest version of NetBeans to compile
simple Java programs as I am learning the languages. I just tried to do a
command line compile & execute and I cannot get it to work so I wend back
to the "Hello World" program with the same result. Here is the program,
the commands I tried, and the error result:
The program file contains:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The Commands:
To Compile: javac k:\HelloWorldApp.java (This worked)
To Execute: java k:\HelloWorldApp (This failed)
The Result:
Exception in thread "main" java.lang.NoClassDefFoundError: k:\HelloWorldApp
Caused by: java.lang.ClassNotFoundException: k:\HelloWorldApp
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
I also tried java K:\NonExistantFile which, of course, failed but with
different errors than the above so it must be finding the .class file but
doesn't like something in it.
What am I doing wrong? Thanks!
Steve W. Jackson - 21 May 2009 15:20 GMT
> I have been using Windows XP & the latest version of NetBeans to compile
> simple Java programs as I am learning the languages. I just tried to do a
[quoted text clipped - 28 lines]
>
> What am I doing wrong? Thanks!
The "java.exe" program doesn't recognize "k:\" as part of a class name
in its default classpath. So the simple solution in this specific
example is to include "-cp k:\" in your command line to tell it that the
root directory of your K drive is the classpath. And then omit "k:\"
from the HelloWorld part in your command. To wit:
java -cp k:\ HelloWorld
Then you might also do a little reading up on the classpath and fully
understand how it works and why you should always specify it on the
command line rather than using a CLASSPATH environment variable.

Signature
Steve W. Jackson
Montgomery, Alabama
Knute Johnson - 21 May 2009 15:33 GMT
> I have been using Windows XP & the latest version of NetBeans to compile
> simple Java programs as I am learning the languages. I just tried to do a
[quoted text clipped - 28 lines]
>
> What am I doing wrong? Thanks!
Because the path to the class file is all tied up with its package. A
simple fix for your problem is;
java -cp k:\ HelloWorldApp
I don't think there is a way to put the Windows drive letter into your
package name. This isn't a problem on Linux as there are no drive
letters. Probably the only time you are going to see this is if you
have a class in the default package on another drive from your current one.

Signature
Knute Johnson
email s/nospam/knute2009/
Roedy Green - 22 May 2009 08:50 GMT
On Thu, 21 May 2009 07:33:18 -0700, Knute Johnson
<nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted
someone who said :
>I don't think there is a way to put the Windows drive letter into your
>package name.
You can't. The thing on the Java command line is not a file, but a
fully qualified dotted class name. You expect it to be the name of
the class file mangled in some way, but it is not.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
"If people become accustomed to lying, they will unconsciously commit every possible wrong deed. Before they can act wickedly, they must lie, and once they begin to lie they will act wickedly without concern."
~ Gautama Buddha
NoSpam@aol.com - 21 May 2009 18:01 GMT
Thanks for the replies. That fixed it.
>I have been using Windows XP & the latest version of NetBeans to compile
>simple Java programs as I am learning the languages. I just tried to do a
[quoted text clipped - 28 lines]
>
>What am I doing wrong? Thanks!
Roedy Green - 21 May 2009 18:45 GMT
> To Execute: java k:\HelloWorldApp (This failed)
That is wishful thinking. See http://mindprod.com/jgloss/javaexe.html
for how it really works.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
"If people become accustomed to lying, they will unconsciously commit every possible wrong deed. Before they can act wickedly, they must lie, and once they begin to lie they will act wickedly without concern."
~ Gautama Buddha