No way to execute program when classes span among different .java files

Discussion in 'Java' started by lonelyplanet999, Nov 25, 2003.

  1. I have below .java files located under c:\javapgm\pack4\

    File: NoPack.java
    -----------------
    package pack4;
    public class NoPack { }
    class Dummy1 {}
    class Dummy2 {}

    File: NoPack2.java
    ------------------
    package pack4;
    public class NoPack2 {
    Dummy3 d3;
    void print1() {
    System.out.println("new Dummy3="+(new Dummy3()));
    }
    public void print2() {
    Dummy4 d4=new Dummy4(); System.out.println("new Dummy4="+d4);
    }
    public void print3() {
    Dummy1 d1 = new Dummy1(); System.out.println("Dummy1 instance
    created in NoPack2 using NoPack.java is "+d1);
    }
    public void print4() {
    System.out.println("Dummy2 instance created in NoPack2 using
    NoPack.java is "+(new Dummy2()));
    }
    }
    class Dummy3 {}
    class Dummy4 {}

    File: AccessLocal.java
    ----------------------
    package pack4;
    public class AccessLocal {
    public static void main (String [] args) {
    System.out.println("Accessing NoPack.java");
    NoPack np = new NoPack();
    System.out.println("NoPack instance "+np+" created");
    NoPack2 np2 = new NoPack2();
    System.out.println("NoPack2 instance "+np+" created");
    Dummy1 d1 = new Dummy1();
    System.out.println("Dummy1 instance "+d1+" created");
    Dummy2 d2 = new Dummy2();
    System.out.println("Dummy2 instance "+d2+" created");
    Dummy3 d3 = new Dummy3();
    System.out.println("Dummy3 instance "+d3+" created");
    Dummy4 d4 = new Dummy4();
    System.out.println("Dummy4 instance "+d4+" created");
    System.out.print("np2.print1() ");
    np2.print1();
    System.out.print("np2.print2() ");
    np2.print2();
    System.out.print("np2.print3() ");
    np2.print3();
    System.out.print("np2.print4() ");
    np2.print4();
    np2.d3 = new Dummy3();
    System.out.println("NoPack2.Dummy3 instance "+np2.d3+" created");
    }
    }

    I called c:\j2sdk1.4.1_01\bin\javac pack4\NoPack.java
    c:\j2sdk1.4.1_01\bin\javac pack4\NoPack2.java
    c:\j2sdk1.4.1_01\bin\javac pack4\AccessLocal.java
    from c:\javapgm, all returned no error.

    However as I called c:\j2sdk1.4.1_01\bin\java pack4\AccessLocal from
    c:\javapgm, java returned exception in thread "main"
    java.lang.NoClassDefFoundError: pack4\AccessLocal (wrong name:
    pack/AccessLocal) and program wasn't started.

    I would like to know why this will happen ?

    Is there no way to run main() if it being defined in a .java file
    belonging to a package having more than one .java file ? I'm sure if I
    placed content of all 3 above .java files in one .java, both
    compilation & execution will succeed. Also, because I want AccessLocal
    class in the same package as others, I couldn't place AccessLocal.java
    in c:\javapgm. Otherwise, the compiler will complain.

    Similar problem appeared in my previously asked question
    http://groups.google.com.hk/groups?...t6lb%246gt%241%40hood.uits.indiana.edu&rnum=1

    where I coded a class Level1 under the file c:\javapgm\l1\.
    Compilation succeeded too but program execution returned exception
    same as above type.

    File: Level1.java, compiled & ran under c:\javapgm similar to above.
    --------------------------------------------------------------------
    package l1;

    public class Level1 {
    public static void main (String [] args) {
    System.out.println("Level 1");
    }
    }

    In this case, program execution always fail on run time. Is there no
    way to associate a package name with a .java where I want it to be
    executed i.e. an runnable main embeded like above ?

    :eek: HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!
     
    lonelyplanet999, Nov 25, 2003
    #1
    1. Advertising

  2. (lonelyplanet999) writes:
    > I would like to know why this will happen ?


    See http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html,
    esp. the "Can't FInd Class" subsection.

    In general I would like to suggest you work through the complete
    beginners's tutorial.

    I would also like to suggest that you post your beginners questions to
    the appropriate newsgroup, which is comp.lang.java.help, and not
    comp.lang.java.programmer.

    >:eek: HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!


    Whining doesn't help.

    http://www.catb.org/~esr/faqs/smart-questions.html

    /Thomas

    --
     
    Thomas Weidenfeller, Nov 25, 2003
    #2
    1. Advertising

  3. Hi,
    As I checked, it worked for me as under.

    - javac.exe -d classes src/pack4/*.java
    // compiles successfully the classes: NoPack.java, NoPack2.java &
    // AccessLocal.java
    - java -cp classes pack4.AccessLocal
    Output:
    Accessing NoPack.java
    NoPack instance pack4.NoPack@1cde100 created
    NoPack2 instance pack4.NoPack@1cde100 created
    Dummy1 instance pack4.Dummy1@360be0 created
    Dummy2 instance pack4.Dummy2@1372a1a created
    Dummy3 instance pack4.Dummy3@126b249 created
    Dummy4 instance pack4.Dummy4@192d342 created
    np2.print1() new Dummy3=pack4.Dummy3@6b97fd
    np2.print2() new Dummy4=pack4.Dummy4@1c78e57
    np2.print3() Dummy1 instancecreated in NoPack2 using NoPack.java is
    pack4.Dummy1@5224ee
    np2.print4() Dummy2 instance created in NoPack2 usingNoPack.java is
    pack4.Dummy2@f6a746
    NoPack2.Dummy3 instance pack4.Dummy3@15ff48b created


    Chandresh

    (lonelyplanet999) wrote in message news:<>...
    > I have below .java files located under c:\javapgm\pack4\
    >
    > File: NoPack.java
    > -----------------
    > package pack4;
    > public class NoPack { }
    > class Dummy1 {}
    > class Dummy2 {}
    >
    > File: NoPack2.java
    > ------------------
    > package pack4;
    > public class NoPack2 {
    > Dummy3 d3;
    > void print1() {
    > System.out.println("new Dummy3="+(new Dummy3()));
    > }
    > public void print2() {
    > Dummy4 d4=new Dummy4(); System.out.println("new Dummy4="+d4);
    > }
    > public void print3() {
    > Dummy1 d1 = new Dummy1(); System.out.println("Dummy1 instance
    > created in NoPack2 using NoPack.java is "+d1);
    > }
    > public void print4() {
    > System.out.println("Dummy2 instance created in NoPack2 using
    > NoPack.java is "+(new Dummy2()));
    > }
    > }
    > class Dummy3 {}
    > class Dummy4 {}
    >
    > File: AccessLocal.java
    > ----------------------
    > package pack4;
    > public class AccessLocal {
    > public static void main (String [] args) {
    > System.out.println("Accessing NoPack.java");
    > NoPack np = new NoPack();
    > System.out.println("NoPack instance "+np+" created");
    > NoPack2 np2 = new NoPack2();
    > System.out.println("NoPack2 instance "+np+" created");
    > Dummy1 d1 = new Dummy1();
    > System.out.println("Dummy1 instance "+d1+" created");
    > Dummy2 d2 = new Dummy2();
    > System.out.println("Dummy2 instance "+d2+" created");
    > Dummy3 d3 = new Dummy3();
    > System.out.println("Dummy3 instance "+d3+" created");
    > Dummy4 d4 = new Dummy4();
    > System.out.println("Dummy4 instance "+d4+" created");
    > System.out.print("np2.print1() ");
    > np2.print1();
    > System.out.print("np2.print2() ");
    > np2.print2();
    > System.out.print("np2.print3() ");
    > np2.print3();
    > System.out.print("np2.print4() ");
    > np2.print4();
    > np2.d3 = new Dummy3();
    > System.out.println("NoPack2.Dummy3 instance "+np2.d3+" created");
    > }
    > }
    >
    > I called c:\j2sdk1.4.1_01\bin\javac pack4\NoPack.java
    > c:\j2sdk1.4.1_01\bin\javac pack4\NoPack2.java
    > c:\j2sdk1.4.1_01\bin\javac pack4\AccessLocal.java
    > from c:\javapgm, all returned no error.
    >
    > However as I called c:\j2sdk1.4.1_01\bin\java pack4\AccessLocal from
    > c:\javapgm, java returned exception in thread "main"
    > java.lang.NoClassDefFoundError: pack4\AccessLocal (wrong name:
    > pack/AccessLocal) and program wasn't started.
    >
    > I would like to know why this will happen ?
    >
    > Is there no way to run main() if it being defined in a .java file
    > belonging to a package having more than one .java file ? I'm sure if I
    > placed content of all 3 above .java files in one .java, both
    > compilation & execution will succeed. Also, because I want AccessLocal
    > class in the same package as others, I couldn't place AccessLocal.java
    > in c:\javapgm. Otherwise, the compiler will complain.
    >
    > Similar problem appeared in my previously asked question
    > http://groups.google.com.hk/groups?...t6lb%246gt%241%40hood.uits.indiana.edu&rnum=1
    >
    > where I coded a class Level1 under the file c:\javapgm\l1\.
    > Compilation succeeded too but program execution returned exception
    > same as above type.
    >
    > File: Level1.java, compiled & ran under c:\javapgm similar to above.
    > --------------------------------------------------------------------
    > package l1;
    >
    > public class Level1 {
    > public static void main (String [] args) {
    > System.out.println("Level 1");
    > }
    > }
    >
    > In this case, program execution always fail on run time. Is there no
    > way to associate a package name with a .java where I want it to be
    > executed i.e. an runnable main embeded like above ?
    >
    > :eek: HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!
     
    Chandresh Prakash, Nov 25, 2003
    #3
  4. lonelyplanet999

    Sudsy Guest

    Re: No way to execute program when classes span among different .javafiles

    lonelyplanet999 wrote:
    > :eek: HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!


    I already sent you the URL of a page which discusses these issues in
    detail. Best learning experience when first using package declarations
    is simply compile with "-d .", i.e.:
    javac -d . <filename>.java
    Take a look at the directory struture generated. That will show you
    how java expects packages to map to the filesystem. If you want to
    read more go to <http://www.sudsy.net/technology/java-classes.html>
    Do some more research before asking the same questions over and over...
     
    Sudsy, Nov 25, 2003
    #4
  5. lonelyplanet999

    Brad BARCLAY Guest

    Re: No way to execute program when classes span among different .javafiles

    lonelyplanet999 wrote:
    > I have below .java files located under c:\javapgm\pack4\
    >
    > File: NoPack.java
    > -----------------
    > package pack4;
    > public class NoPack { }
    > class Dummy1 {}
    > class Dummy2 {}


    This is very bad form. You should only define multiple classes in a
    single source file _if_ they are inner or anonymous classes. Otherwise,
    use one source file per class file. By doing so, you'll find that
    finding the solutions to your problems yourself becomes quite a bit
    easier, and the resulting code becomes more maintainable (particularily
    if multiple people are working on the project).

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
     
    Brad BARCLAY, Nov 25, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MK
    Replies:
    6
    Views:
    717
    noSpAm0000
    May 16, 2005
  2. Fulio Open

    Can span include span?

    Fulio Open, Jun 26, 2009, in forum: HTML
    Replies:
    5
    Views:
    576
    dorayme
    Jun 26, 2009
  3. Stéphane Klein
    Replies:
    2
    Views:
    1,855
    John Nagle
    Mar 30, 2010
  4. Stefan Behnel
    Replies:
    0
    Views:
    503
    Stefan Behnel
    Mar 29, 2010
  5. Dan Bishop

    DataGrid (body only) contained in <span>...</span> tags

    Dan Bishop, Jun 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    306
    Kilic Beg
    Jun 7, 2004
Loading...

Share This Page