printemp
printemp

Reputation: 879

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

Upvotes: 0

Views: 655

Answers (4)

andiwand
andiwand

Reputation: 86

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>

Upvotes: 0

user330315
user330315

Reputation:

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

Upvotes: 2

Alireza Jafari
Alireza Jafari

Reputation: 42

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

java -cp H:\deleteFiles\delete

Upvotes: 0

Kayaman
Kayaman

Reputation: 73578

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

Upvotes: 0

Related Questions