简体   繁体   中英

Error: Could not find or load main class OSNameGenerator

I created a simple java class which gets the name of the operating system. I also created a batch file to run the java class with the bundled JAVA 7. It seemed to work fine in my system. But when I copied this file and ran the script in another system it threw 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. But even simply running java file only from the cmd also is not working.

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

    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.

Here is my updated script

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

pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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