简体   繁体   中英

run java file in windows command prompt

I want to run a java project in windows. I first compiled the .class file in linux. Copy back to windows. Now under the path H:\\deletefiles has delete.class, delete.java, a.jar, b.jar . The package for class delete is deleteFiles .

My java class path is C:\\program Files\\Java\\jre7\\bin , Where I have no access to write.

I run in command prompt C:\\program Files\\Java\\jre7\\bin>

java -cp H:\deleteFiles\deleteFiles.delete 

always has the problem could not find or load main class, what's the problem? thanks

You are missing the actual class to be run. The -cp H:\\deleteFiles\\deleteFiles.delete only defines the classpath to be used, but not which class you want to run (and you limit the classpath to a single class as well).

What you want is:

java -cp H:\deleteFiles\deleteFiles delete

Note the blank (space) between H:\\deleteFiles\\deleteFiles which means you are passing two parameters to the java command:

  1. -cp H:\\deleteFiles\\deleteFiles - the classpath to use
  2. delete - the class to run

If you need the classes that are part of the jar files, you need to add them to the classpath as well:

java -cp H:\deleteFiles\deleteFiles;H:\deleteFiles\deleteFiles\a.jar;H:\deleteFiles\deleteFiles\b.jar delete

you should call delete.class in your java command line, like this:

java -cp H:\\deleteFiles\\delete

You need to set the classpath to the location which contains the package hierarchy. If your package is named deleteFiles the location needs to contain a directory named deleteFiles which contains the class file.

In your example you would run it with java -cp H:\\ deleteFiles.delete

To execute a Java program you have two options. Using a class file or using a jar file. If your program only contains a single source file, executing the class file would be fine. But if you have multiple sources, you would have to copy all of them. Then a jar would be more practicable.

For class:

java -cp <class path> <class name>

For jar (if the main class is set):

java -jar <jar file>

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