简体   繁体   中英

Running Java Program From Command Line

so I am having a noob moment here, I haven't ever used the command line to run a java program before but I need to right now. The problem I am having is that when I try to run the program I get a ClassNotFoundException. My class is called OmadUpdate. I already have compiled the OmadUpdate.java file into OmadUpdate.class using the javac command. I have checked the directory and they are both definitely there, however when I run the java OmadUpdate command, it gives me an error message saying

Exception in thread "main" java.lang.NoClassDefFoundError: OmadUpdate (wrong name:     org/openmetadata/main/OmadUpdate)
......
......
Could not find the main class: OmadUpdate. Program will exit

But its right there in the directory. When I type dir I have both OmadUpdate.class and OmadUpdate.java. I have even tried using "java org.openmetadata.main.OmadUpdate" because that is the package name that it is under. I am stumped. Thanks for the assistance.

Your class appears to have been declared in the org.openmetadata.main package.

To get java to load the class correctly, it needs to be in the correct directory structure that matches the package structure.

So the classfiles for org.openmetadata.main.OmadUpdate should be in the directory org\\openmetadata\\main .

Then, when you run the java command, the root of this directory structure should be on the classpath - for a simple example this just means that your current directory should be the parent directory of org\\openmetadata\\main .

When running java you need to specify the full classname using periods not slashes, ie

java org.openmetadata.main.OmadUpdate

After you compile the class with javac, you'll have the following directory structure:

org/
  openmetadata/
    main/
      OmadUpdate.class
      OmadUpdate.java

Make sure you're in the parent directory of org, then run

java -cp . org.openmetadata.main.OmadUpdate

Class names have their nested package names separated by periods, while the directories use slashes. Odds are good you tried java -cp . org/openmetadata/main/OmadUpdate java -cp . org/openmetadata/main/OmadUpdate when you should have (since you are specifying a class name) tried java -cp . org.openmetadata.main.OmadUpdate java -cp . org.openmetadata.main.OmadUpdate

Note that for this to work, you must run it in the directory just above the org subdirectory. Otherwise that classpath directive cp . will start the search in the wrong directory.

使用类路径集启动您的Java应用程序:

java -cp . org.openmetadata.main.OmadUpdate

-cp . won't do anything I don't think. You have to make sure you are invoking java in the right directory, which is the part to the first package name / folder (in your case org)

You need to use the full package and class name to run it.

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