从 VB.Net 执行 Java 类

发布于 2024-10-15 05:15:52 字数 950 浏览 5 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昨迟人 2024-10-22 05:15:52

您需要向其添加 -classpath。基本上Java解释器不知道在哪里可以找到这个“Main”类。

You need to add -classpath to it. Basically the Java interpreter does not know where to find this "Main" class.

眼眸里的快感 2024-10-22 05:15:52

CoolBean 的解决方案应该可以工作。就像

Shell("cmd.exe /k java -classpath %TEMP% Main")

因为你可以(并且应该)省略 .class 扩展名。正如 CoolBeans 所说,您将类文件所在的目录设置为类路径。

javac 接受一个文件,java 接受一个类(换句话说,“public class”后面的类的名称),以及 packages 它位于(如果您没有“package some;”) java 文件的顶部,不用担心这一点),它会在您提供的类路径或当前工作目录中查找该类。

如果这确实是解决方案,请向 CoolBeans 提供可接受的答案。

但是,另一种解决方案是将 Shell 的当前工作目录更改为 %TEMP%,例如:

IO.Directory.SetCurrentDirectory(Environ("TEMP"))
Shell("cmd.exe /k java Main")

或者查看 Process 类,它提供对启动其他程序的更精细的控制(并且使用 Process 您还可以更改您的程序的目录)重新启动而不更改您自己的应用程序的当前目录)。

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

我只土不豪 2024-10-22 05:15:52

试试这个,

Shell("java.exe -cp .;" & Environment.GetEnvironmentVariable("TEMP") & " Main")

或者

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文