Vim: running java from vim command gives error
I wrote a simple hello world program to test the JDK installation. I can compile from Vim using:
:!javac DesktopHelloWorld.java
That works just fine but when I try to run the program using:
:!java DesktopHelloWorld
it gives me this error:
C:Windowssystem32cmd.exe /c java "DesktopHelloWorld" Exception in thread "main" java.lang.NoClassDefFoundError: DesktopHelloWorld (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: DesktopHelloWorld. Program will exit.The program works just fine from the normal command line.
C:UsersCaseyDesktop>java HelloWorld
Hello world!
How can I execute the program from Vim without having to switch to the standard cmd?
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
The culprit is this line:
java DesktopHelloWorld
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 DesktopHelloWorld), 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).
When you run java from the command line are you also running java DesktopHelloWorld?
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
链接地址: http://www.djcxy.com/p/38874.html上一篇: Hadoop MAC OS安装困境