Results 1 to 4 of 4
  1. #1
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Help with packages

    I've written a java application with several classes all in the same .java file. It works just fine. Now, I've broken it up so that each class has its own .java file. Still works fine. My next step is to put those classes into a package, but I'm not about to get the program to run.

    The .java source files are all in /home/user/src

    I've set the CLASSPATH to /home/user/src

    All of the source files have "package com.myfirm.program" on the first line.

    I compiled the application with:
    javac -d . File1.java File2.java File3.java (etc...)

    the compiler created the directory:
    /home/user/src/com/myfirm/program

    and put all of the .class files in there.

    So how do I get the program to run?

    if I run from /home/usr/src
    java File1
    I get:
    Exception in thread "main" java.lang.NoClassDefFoundError: File1 (wrong name: com/myfirm/program/Program)

    Any help would be appreciated.
    Last edited by Boomn4x4; 04-09-2015 at 06:14 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with packages

    The folder structure under your src directory should match the package(s) you use, and the .java files should sit in those folders, not in the src folder directly.

    That's the first thing. It might have worked for you now, but your current source layout will eventually come back and bite you.

    Then, once you've compiled (using javac <path to file>/<filename>) then you can execute the code using:
    java <full class name>

    Now the "full class name" includes the package, so com.myfirm.program.<classname> in your case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help with packages

    So I've taken my main program, the one that had the actual public static void main in it, out of the package. I then put an import com.myfirm.program.*; in it.

    I've moved all of the .java files to src/com/myfrim/program

    I deleted all of the .class files in src/com/myfrm/prog and recompiled. All .class files are back in com/fim/prog

    I moved my main program back to src/

    When I try to complile javac File1.java I get:
    error: cannot access MessageLoop
    Thread msgLoop = new Thread(new MessageLoop());
    ^
    bad class file: /src/com/myfirm/program/MessageLoop.class
    class file contains wrong class: com.myfirm.program.MessageLoop
    Please remove or make sure it appears in the correct subdirectory of the class

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with packages

    Why did you move the main class?
    All classes should be in packages, IMO.
    Not doing that can cause problems with certain frameworks, so getting into the habit early is a Good Thing.

    Where are you executing javac from?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Packages in our app
    By Nazneen Ali in forum Android
    Replies: 14
    Last Post: 11-08-2013, 03:52 PM
  2. com.sun packages
    By Aileen in forum JavaFX
    Replies: 1
    Last Post: 03-13-2012, 09:58 PM
  3. How to use my own packages?
    By Vardiane in forum New To Java
    Replies: 4
    Last Post: 04-22-2011, 04:53 AM
  4. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  5. Replies: 3
    Last Post: 02-12-2008, 11:03 AM

Posting Permissions

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