Search code examples
javacmdclassnotfound

JAVA Newbie Alert: Trying to run Java class and getting ClassNotFoundException


Java newbie. I am trying to run a java main class from cmd line and getting back ClassNotFoundException:

java -cp class c:\development\eclipse\workspace\Library-Manager-Server\bin\demo\rmi\librarymanager\server\LibraryManagerServer 

-Djava.security.policy="C:\Development\Eclipse\Workspace\Library-Manager-Server\security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin/ file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin/"

The class is definitely there in the bin (where eclipse drops it) but for some reason (probably trivial) it cannot be found when I run the cmd line above.

Any help appreciated!

EDIT#1: as some of you may have guessed classname is demo.rmi.librarymanager.server.LibraryManagerServer

EDIT#2: OK - thanks to people suggestions I think I got the syntax right this time but I am still getting ClassNotFoundException but on a different class in the common package:

demo.rmi.librarymanager.common.Library

Here's the new cmd line:

java -cp c:/development/eclipse/workspace/Library-Manager-Server/bin demo.rmi.librarymanager.server.LibraryManagerServer 

-Djava.security.policy="C:/Development/Eclipse/Workspace/Library-Manager-Server/security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin"

Shall I add the c:/development/eclipse/workspace/Library-Manager-Common/bin path (where demo.rmi.librarymanager.common.Library lives) together with c:/development/eclipse/workspace/Library-Manager-Server/bin after -cp as a command line argument?

Thanks to everyone for their help and patience - I am working on an RMI application and everything was good till I was working inside eclipse using the RMI plugin but now I am having a bit of an hard time as I am fairly new to java (as you cas see from my question!).

EDIT#3: Ok I got it working:

java -cp c:/development/eclipse/workspace/Library-Manager-Server/bin;c:/development/eclipse/workspace/Library-Manager-Common/bin 

demo.rmi.librarymanager.server.LibraryManagerServer 

-Djava.security.policy="C:/Development/Eclipse/Workspace/Library-Manager-Server/security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin"

Thanks to everyone for the shower of humbleness.


Solution

  • The java commandline is:

    Usage: java [-options] class [args...]
               (to execute a class)
       or  java [-options] -jar jarfile [args...]
               (to execute a jar file)
    
    where options include:
        -client       to select the "client" VM
        -server       to select the "server" VM
        -hotspot      is a synonym for the "client" VM  [deprecated]
                      The default VM is client.
    
        -cp <class search path of directories and zip/jar files>
        -classpath <class search path of directories and zip/jar files>
                      A ; separated list of directories, JAR archives,
                      and ZIP archives to search for class files.
    

    I think I see your problem - 'class' is not a keyword; it is the fullyQualified classname that you have to replace it with.

    If your class files are in bin then the command might look like:

    java -cp bin myClass
    

    so perhaps

    java -cp bin demo.rmi.librarymanager.server.LibraryManagerServer
    

    if you are in the directory of your Eclipse project.

    Note that Eclipse has settings (which you can override) for where the classes are. I have found that different versions of Eclipse have different ones by default. So I have seen bin, classes or (as I am using maven) target/classes.