Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Trouble Running Program from Command Prompt

  1. #1
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Trouble Running Program from Command Prompt

    Hey guys, this question might seem familiar because I was having trouble compiling classes and posted a thread that involves some of the same classes.

    Basically, I have 3 classes: Item, Storefront, and GiftShop. Item and Storefront exist in a package called com.skyreign.store. GiftShop exists in a package called com.skyreign.shop.

    Storefront uses Item. GiftShop uses Storefront and Item. I have successfully compiled all 3 files.

    I can successfully run GiftShop by typing this into the command prompt:

    Java Code:
    C:\Programming\Java\bin>java com.skyreign.shop.GiftShop
    However, if I move up a directory by typing this:

    Java Code:
    C:\Programming\Java\bin>cd ..\
    And then try to run GiftShop again by typing this:

    Java Code:
    C:\Programming\Java>java bin\com.skyreign.shop.GiftShop
    I receive this output:

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: bin\com/skyreign/shop
    /GiftShop (wrong name: com/skyreign/shop/GiftShop)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$100(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    It seems that it successfully found the main class of GiftShop, which I think is good But I don't understand what the exception is trying to tell me. I have searched around this forum for similar threads and I have deduced that it has something to do with my classpath being off.

    I realize that I can run the class successfully, but I'm not satisfied because I want to know exactly what is going on. I think it's important to get a grasp of running and compiling files before I move on.

    Any help would be appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Trouble Running Program from Command Prompt

    The parameter to the java command is the full class name (including package).
    Your class is not called 'bin\com.skyreign.shop.GiftShop'.

    If you want to run it from another directory than the bin directory then you will need to provide a classpath that includes that bin directory, eg:
    java -cp C:\Programming\Java\bin com.skyreign.shop.GiftShop
    which will tell the JVM that it needs to look for the classes to run in the given directory.
    awinston likes this.
    Please do not ask for code as refusal often offends.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Trouble Running Program from Command Prompt

    Thank you so much! I just want to clarify something about the classpath, though.

    When using the javac tool in order to compile a source file, I need to specify a classpath that contains the class files that are referenced in the source file that I am currently trying to compile.

    Ex: Storefront references Item. Item.class is located in the bin folder. If I want to compile the Storefront source file, then typing this into the command prompt works:

    Java Code:
    C:\Programming\Java>javac -d bin -cp bin src\com\skyreign\store\Storefront.java
    But if I want to run the Storefront.class file, I need to specify a classpath that contains Item.class again?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Trouble Running Program from Command Prompt

    Java needs to know where the classes are.
    It takes the value supplied to '-cp' first, if none is defined it then takes any CLASSPATH defined (note, don't define this in your environment variables, batch files is OK, but global is a Bad Thing), finally defaulting to '.'.
    So yes, you'd need to tell it where the classes are if you plan on running anywhere other than that bin directory.
    Please do not ask for code as refusal often offends.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Trouble Running Program from Command Prompt

    Okay, that makes sense. Any time a class is used, Java needs to know where it is located.

    I'm just curious, though. What is wrong with defining a CLASSPATH as a global environment variable?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Trouble Running Program from Command Prompt

    What if you had several Java applications, each needing their own classpath?
    Please do not ask for code as refusal often offends.

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Trouble Running Program from Command Prompt

    That would be inconvenient, then. Ok, thanks.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Trouble Running Program from Command Prompt

    I've seen it happen in live deployment environments before, where someone clearly thought "we'll only ever need version X of these jars" and set up a server-wide classpath. And then another app gets deployed that needs a different version, and then another and another...how we laughed...
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Running .jar in command prompt
    By wonstatus in forum New To Java
    Replies: 7
    Last Post: 07-17-2011, 11:35 PM
  2. java program for running the command prompt commands
    By leoanto in forum Advanced Java
    Replies: 2
    Last Post: 03-26-2011, 03:57 PM
  3. Compiling/Running in Command Prompt
    By coke_in_a_can in forum New To Java
    Replies: 12
    Last Post: 05-31-2010, 09:05 AM
  4. running command prompt using java
    By xclaim in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 03:46 AM
  5. Running applet from command prompt
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •