22

I am not very experience with java and this is driving me crazy. I wrote a java program FileManagement and I need to run it from the command line.

I can compile it from the command line with javac FileManagement/*.java which will create all the classes in that folder but when I try java FileManagement.Main it says :

Exception in thread "main" java.lang.NoClassDefFoundError: FileManagement/Main

The thing is that I have tried this same procedure in a remote computer and it is working fine. It is not working on mine.

2
  • skylit.com/javamethods/faqs/javaindos.html has a good intro for beginners. Commented Jun 20, 2013 at 8:00
  • @Pramod. That link is for windows though... Commented Sep 16, 2014 at 8:19

5 Answers 5

21

If your Main class is in a package called FileManagement, then try:

java -cp . FileManagement.Main

in the parent folder of the FileManagement folder.

If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:

java -cp . Main

More info about the CLASSPATH and how the JRE find classes:

Sign up to request clarification or add additional context in comments.

2 Comments

@Altober, good to hear that. I encourage you to read a bit about the classpath so you know why it didn't work.
@AnsersonGreen, just provide the parameters after Main.
7

Guys let's understand the syntax of it.

  1. If class file is present in the Current Dir.

    java -cp . fileName

  2. If class file is present within the Dir. Go to the Parent Dir and enter below cmd.

    java -cp . dir1.dir2.dir3.fileName

  3. If there is a dependency on external jars then,

    java -cp .:./jarName1:./jarName2 fileName

    Hope this helps.

Comments

4

(This is the KISS answer.)

Let's say you have several .java files in the current directory:

$ ls -1 *.java
javaFileName1.java
javaFileName2.java

Let's say each of them have a main() method (so they are programs, not libs), then to compile them do:

javac *.java -d .

This will generate as many subfolders as "packages" the .java files are associated to. In my case all java files where inside under the same package name packageName, so only one folder was generated with that name, so to execute each of them:

java -cp . packageName.javaFileName1
java -cp . packageName.javaFileName2

Comments

1

What is the package name of your class? If there is no package name, then most likely the solution is:

java -cp FileManagement Main

3 Comments

Thanks for your time Bart K gave the same solution and It worked. Thanks again Thomas
If the main class was named "Main" (without package), and if the class was simply in the directory named "FileManagement". The same as cd FileManagement ; java cp . Main
Ah, sorry, you added FileManagement to the classpath. I thought you tried to execute FileManagement and gave Main as a command line parameter.
0

One liner for a single .java file called Main.java:

javac Main.java && java -cp . Main

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.