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.
Guys let's understand the syntax of it.
If class file is present in the Current Dir.
java -cp . fileName
If class file is present within the Dir. Go to the Parent Dir and enter below cmd.
java -cp . dir1.dir2.dir3.fileName
If there is a dependency on external jars then,
java -cp .:./jarName1:./jarName2 fileName
Hope this helps.
If your
Main
class is in a package calledFileManagement
, then try:in the parent folder of the
FileManagement
folder.If your
Main
class is not in a package (the default package) then cd to theFileManagement
folder and try:More info about the CLASSPATH and how the JRE find classes:
(This is the KISS answer.)
Let's say you have several .java files in the current directory:
Let's say each of them have a
main()
method (so they are programs, not libs), then to compile them do: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:What is the package name of your class? If there is no package name, then most likely the solution is: