I have a class in java "Main.class", wrote and stored in %TEMP%. When executing the class through VB.Net Shell, eg:
Shell("cmd.exe /k java %TEMP%\Main.class")
Also when trying to execute manually through CMD: "java %TEMP%\Main.class", I am returned with:
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\Ben\AppData\
Local\Temp\Main/class
Caused by: java.lang.ClassNotFoundException: C:\Users\Ben\AppData\Local\Temp\Mai
n.class
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)
Could not find the main class:开发者_运维问答 C:\Users\Ben\AppData\Local\Temp\Main.class. Prog
ram will exit.
However, when I execute Main.class manually through compile.bat - the class runs fine. What is the reasoning for this?
You need to add -classpath to it. Basically the Java interpreter does not know where to find this "Main" class.
CoolBean's solution should work. Something like
Shell("cmd.exe /k java -classpath %TEMP% Main")
Since you can (and are supposed to) omit the .class extension. And like CoolBeans said, you set the directory your class file lives in as the classpath.
While javac takes a file, java takes a Class (in other words, the name of the class after 'public class'), along with what packages it is in (if you don't have "package something;" at the top of your java file, don't worry about this), and it'll look for that class in the classpath you provide, or the current working directory.
If that does end up being the solution, give CoolBeans the accepted answer.
However, an alternative solution is to change the current working directory for Shell to %TEMP%, like:
IO.Directory.SetCurrentDirectory(Environ("TEMP"))
Shell("cmd.exe /k java Main")
Or alternatively look into the Process class, which offers more fine control over launching other programs (and with Process
you can also change the directory of the program you're launching without changing the current directory of your own application).
Try this,
Shell("java.exe -cp .;" & Environment.GetEnvironmentVariable("TEMP") & " Main")
OR
Dim args As String = String.Format("-cp .;{0} {1}", Environment.GetEnvironmentVariable("TEMP"), "Main")
Dim procInfo As New ProcessStartInfo
procInfo.FileName = "java.exe"
procInfo.Arguments = args
Dim proc As New Process
proc.StartInfo = procInfo
proc.Start()
proc.WaitForExit()
精彩评论