简体   繁体   English

错误:无法找到或加载主类 OSNameGenerator

[英]Error: Could not find or load main class OSNameGenerator

I created a simple java class which gets the name of the operating system.我创建了一个简单的 java 类,它获取操作系统的名称。 I also created a batch file to run the java class with the bundled JAVA 7. It seemed to work fine in my system.我还创建了一个批处理文件来运行带有捆绑的 JAVA 7 的 java 类。它在我的系统中似乎运行良好。 But when I copied this file and ran the script in another system it threw Error: Could not find or load main class OSNameGenerator .但是当我复制这个文件并在另一个系统中运行脚本时,它抛出了Error: could not find or load main class OSNameGenerator

At first I thought This may have to do something with the script and that it is not able to find the bundled java due to some issues in path.起初我认为这可能与脚本有关,并且由于路径中的某些问题而无法找到捆绑的java。 But even simply running java file only from the cmd also is not working.但即使只是从 cmd 运行 java 文件也行不通。

I know this feels is so simple yet I'm not able to find what is creating the issue here.我知道这感觉很简单,但我无法在这里找到造成问题的原因。

Here is my java class这是我的java类

    public class OSNameGenerator{



    public static void main(String[] args){

        System.out.println(System.getProperty("os.name"));
    }


}

and This my batch script这是我的批处理脚本

"%~dp0\jre\bin\java.exe"  OSNameGenerator

 pause

And this is my file structure这是我的文件结构

在此处输入图片说明

Please help me to find out what is wrong with this.请帮我找出这有什么问题。

This seems to be an issue with classpath.这似乎是类路径的问题。

I fixed it by changing the classopath to current directory using (.) operator in my scrpit.我通过在我的 scrpit 中使用 (.) 运算符将类路径更改为当前目录来修复它。

Here is my updated script这是我更新的脚本

.\jre\bin\java.exe -cp . OSNameGenerator

pause

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

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