2
votes

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?

4

4 Answers

5
votes

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
2
votes

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

1
votes

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

1
votes

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.