views:

329

answers:

4

I wrote a simple hello world program to test the JDK installation. I can compile from Vim using:

:!javac Desktop\HelloWorld.java

That works just fine but when I try to run the program using:

:!java Desktop\HelloWorld

it gives me this error:

C:\Windows\system32\cmd.exe /c java "Desktop\HelloWorld" Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\HelloWorld (w rong name: HelloWorld) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) 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) Could not find the main class: Desktop\HelloWorld. Program will exit.

The program works just fine from the normal command line.

C:\Users\Casey\Desktop>java HelloWorld
Hello world!

How can I execute the program from Vim without having to switch to the standard cmd?

+1  A: 

When you run java from the command line are you also running java Desktop\HelloWorld?

As a first suggestion I would suggest to try running the following so that you compile and execute your program from the same directory:

:!cd Desktop :!javac HelloWorld.java :!java HelloWorld

chocojosh
+5  A: 

java doesn't take a file name as its first non-option argument. Instead it should specify the main class name (with .s between package names and class name, if there were any packages other than a default). Use -classpath to specify where to load classes from (with directories separated with your OS's directory separator character (\)).

:!java -classpath Desktop HelloWorld
Tom Hawtin - tackline
isn't it !java -classpath Desktop.HelloWorld ?looks like SO ate your period
GoingTharn
HelloWorld is the class, Desktop the Path in which it can be found. Also it appears that the class uses the default package (otherwise java HelloWorld wouldn't work). So the lack of the dot there is indeed correct.
Joey
I assumed so. `Desktop` has an initial cap which is wrong for a package. `Desktop` is also a directory I'd expect to find on Windows.
Tom Hawtin - tackline
+1  A: 

I am going to take an educated guess here, but vim might not be recognizing the path variable, like calling java from the command line will. Of course, if you are going to do a good deal of Java coding in vim, you might want to take a look at this plugin.

Javed Ahamed
That was my first guess, but if you look at the output (Exception in thread main) you can see it is calling the proper exe (otherwise there`d be a generic error such as: 'java' is not recognized as an internal or external command, operable program or batch file.
chocojosh
+2  A: 

The culprit is this line:

java Desktop\HelloWorld

The argument that you are passing to the "java" program is a class name, not a path name. If the file is in package "Desktop.HelloWorld" (directory Desktop\HelloWorld), then you need to execute it with:

java Desktop.HelloWorld

(all of the above assumes that you are in the folder directly above the "Desktop" folder).

jsight