简体   繁体   English

从C#程序启动时.jar将无法运行

[英].jar won't run when launched from C# program

I'm using C# to develop some programs for Kinect. 我正在使用C#为Kinect开发一些程序。 C# doesn't have anything as good as Java's Robot for simulating Keystrokes or Mouse movements, so I'm using Java for that. C#在模拟按键或鼠标移动方面不如Java的机器人好,因此我在使用Java。 At the moment, I'm creating .jar files and trying to run them from the C# application (although I'm suspicious that there's a better way to do it). 目前,我正在创建.jar文件,并尝试从C#应用程序运行它们(尽管我怀疑有更好的方法来执行此操作)。 The way I do this is by putting this line in my C# code: 我这样做的方法是将这一行放入我的C#代码中:

System.Diagnostics.Process.Start("CMD.exe", java -jar C:\\Users\\Me\\RobotProgram.jar");

This works fine in a small, basic C# application: 在一个小的基本C#应用程序中,这可以正常工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           System.Diagnostics.Process.Start("CMD.exe", "/c java -jar C:\\Users\\Me\\RobotProgram.jar");
        }
    }
}

But when it's in a more complex program that uses the Kinect camera, it won't work. 但是,当它在使用Kinect相机的更复杂的程序中时,它将无法工作。 The console flashes up saying "Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object". 控制台闪烁,显示“ VM VM / lang / NoClassDefFoundError初始化期间发生错误:java / lang / Object”。

The Java project may be looking for required files in the working directory. Java项目可能正在工作目录中寻找所需的文件。 Try setting the working directory before launching. 启动前,请尝试设置工作目录。

Directory.SetCurrentDirectory("C:\\Users\\Me");
System.Diagnostics.Process.Start("CMD.exe", "/c java -jar C:\\Users\\Me\\RobotProgram.jar");

You may need to specify the application's entry point. 您可能需要指定应用程序的入口点。

Here's a simple explanation: https://samindaw.wordpress.com/2008/11/04/specifying-the-main-class-to-run-in-a-jar-file-from-command-line/ 这是一个简单的解释: https : //samindaw.wordpress.com/2008/11/04/specifying-the-main-class-to-run-in-a-jar-file-from-command-line/

Do you have your JAVA_HOME environmental variable set up properly in the environment where the complex program is running? 您是否在运行复杂程序的环境中正确设置了JAVA_HOME环境变量?

The error points to an empty/wrong JAVA_HOME as even the base classes are not found 错误指向一个空/错误的JAVA_HOME,因为甚至找不到基类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM