Results 1 to 9 of 9
  1. #1
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default class path problem?

    I thought I understood the process of creating packages, but I have been unable to make this application work when I try to create an application using files in multiple directories:
    This is my directory tree
    L:\JAVAHW
    |___rdt
    |___Prompts/ (java files)
    |___PayRoll/ (java files)

    This is my imports and package definition in one of the applications in the
    PayRoll directory

    package rdt.PayRoll;
    import rdt.Prompts.Prompts;
    import java.util.Scanner;
    import java.io.BufferedWriter;

    the package statement for the one file in Prompts is
    package rdt.Prompts

    I can compile all files without error, as seems to be typical with this type of
    problem.

    I clear and set the class path with a .bat file (Vista OS)
    set CLASSPATH=
    set CLASSPATH=.;L:\javahw\;

    This is the output I get when I try to run the app.

    java.lang.NoClassDefFoundError: PayDay_App (wrong name: rdt/PayRoll/PayDay_App) (and more)

    The program was developed in eclipse using two different packages, and it works when I put everything in the same directory. Putting a package statement in a HelloWorld app works correctly: java rdt.Payroll.Hello displays.

    This seems so straight forward and I reviewed the sun tutorial. I am completly stuck.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro at command line compiling and running, but perhaps your using javahw instead of JAVAHW is of issue. Myself, I generally don't set the OS's CLASSPATH property as you are doing, but instead use a java -cp option.

    I recommend that you show us exactly how you are trying to run your program, what you type in (you don't use the .class extension here do you?) and then show us the exact error message(s) that you receive.

    Much luck!
    Last edited by Fubarable; 11-15-2009 at 05:05 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Specify the complete class name when you run the app (PayDay_App could be class in any package whatsoever).

    Java Code:
    java rdt.PayRoll.PayDay_App
    (often you would specify the classpath as part of the command you use to run the application, and would start package names with a lower case letter.)

  4. #4
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    java.lang.NoClassDefFoundError: PayDay_App (wrong name: rdt/PayRoll/PayDay_App)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: PayDay_App. Program will exit.
    Exception in thread "main"

    is the error message.

    The capitalization of JAVAHW was something I hadn't considered. When I looked at it, the capitalization was created by the dos tree command. It is actually in small case on the drive.

    I have been using the full package name as was indicated in the portion of the error message. The rest of the message is just gobbledy-gook to me.

    My understanding is that if the code is in c:\foo\bar\foobar.java and the package is bar,
    then the class path should be c:\foo and the command line should be java bar.foobar.
    I am running the program from the PayRoll director using java rdt.PayRoll.PayRoll_App, which is the name parroted in the error message.

    I've also played around with using -cp and -classpath switches.

    I'll try one more thing. I've assumed that all the code I need to run the app is in the application file, so that was the only path I put in the path statement. If the Prompt class is needed at runtime, it would need the class path to that file.

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default Problem solved

    Ran the command line from javahw, not javahw\rdt\PayRoll, and it worked.
    Solved

  6. #6
    Swamipsn is offline Member
    Join Date
    Jul 2007
    Posts
    74
    Rep Power
    0

    Default

    I would advice all Java newbees to use IDE

  7. #7
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I agree. Many of the questions posted on forum could be solved using a debugger.

    In this case, the files were part of a homework assignment that has to be handed in. I think that my partitioning was appropriate, and simply wished to pass the file structure to the instructor who can compile the files and run from his own directory. Using packages should allow him to do so. Perhaps a .jar would be better, but I haven't studied that process yet.

    I am bothered that I have to change directory to the end of the classpath to make things run. With the CLASSPATH environment set, and the structure of the package, java should be able to run an application from any location.

    FWIW, advice is a noun. "I would advise you to take this advice." I know I make my own share of typos, and occasionally use their or there when I know I mean they're.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rdtindsm View Post
    I am bothered that I have to change directory to the end of the classpath to make things run. With the CLASSPATH environment set, and the structure of the package, java should be able to run an application from any location.
    It can do this.

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

    Default

    Quote Originally Posted by Swamipsn View Post
    I would advice all Java newbees to use IDE
    I don't.

    Understanding the classpaths, packages and how they map to the underlying directory structure is very important. I've seen too many questions from people who have only used IDEs regarding class not found exceptions, or runtime problems when deploying their app outside of an IDE...questions they would have known the answer to if they'd learned the basics first.

Similar Threads

  1. how to set the class path
    By lashu in forum New To Java
    Replies: 1
    Last Post: 08-21-2009, 01:04 PM
  2. Cannot find class in same path.
    By ribbs2521 in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 05:25 PM
  3. trying to get class name & path
    By sandor in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 01:38 PM
  4. Setting up class path for Eclipse
    By tsantana in forum Eclipse
    Replies: 1
    Last Post: 04-07-2008, 07:05 PM
  5. Log4J.jar in class path
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-02-2008, 06:39 PM

Posting Permissions

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