0

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

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

If they contain classes that your class needs, then yes.
sorry to disturb again... if I add the package name java -cp H:\deleteFiles\deleteFiles deleteFiles.delete, it will still no class find. If dismiss the package, it will complain Exception in thread "main" java.lang.NoClassDefFoundError: (wrong nam e: deleteFiles/delete) .....
If delete.java specifies package deleteFiles, then you need to specify the directory containing the package as the classpath: -cp H:\deleteFiles. This is all described in the Java tutorials: docs.oracle.com/javase/tutorial/index.html
0

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

Comments

0

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

java -cp H:\deleteFiles\delete

2 Comments

@a_horse_with_no_name. Yes, we do not need. but what's the problem?I always do this in linux, no problem
that's correct, i edit my question and remove .class extension,thanks
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.