从 VB.Net 执行 Java 类
我在java“Main.class”中有一个类,编写并存储在%TEMP%中。当通过 VB.Net Shell 执行该类时,例如:
Shell("cmd.exe /k java %TEMP%\Main.class")
另外,当尝试通过 CMD 手动执行时:“java %TEMP%\Main.class”,我会返回:
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.
但是,当我通过compile.bat 手动执行 Main.class 时- 课程进展顺利。这是什么原因呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要向其添加 -classpath。基本上Java解释器不知道在哪里可以找到这个“Main”类。
You need to add -classpath to it. Basically the Java interpreter does not know where to find this "Main" class.
CoolBean 的解决方案应该可以工作。就像
因为你可以(并且应该)省略 .class 扩展名。正如 CoolBeans 所说,您将类文件所在的目录设置为类路径。
javac 接受一个文件,java 接受一个类(换句话说,“public class”后面的类的名称),以及 packages 它位于(如果您没有“package some;”) java 文件的顶部,不用担心这一点),它会在您提供的类路径或当前工作目录中查找该类。
如果这确实是解决方案,请向 CoolBeans 提供可接受的答案。
但是,另一种解决方案是将 Shell 的当前工作目录更改为 %TEMP%,例如:
或者查看 Process 类,它提供对启动其他程序的更精细的控制(并且使用
Process
您还可以更改您的程序的目录)重新启动而不更改您自己的应用程序的当前目录)。CoolBean's solution should work. Something like
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:
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,
OR