Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > No way to execute program when classes span among different .java files

Reply
Thread Tools

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

 
 
lonelyplanet999
Guest
Posts: n/a
 
      11-25-2003
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?h...edu%26rnum%3D1

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 ?

HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      11-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (lonelyplanet999) writes:
> I would like to know why this will happen ?


See http://java.sun.com/docs/books/tutor...ems/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.

> HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!


Whining doesn't help.

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

/Thomas

--
 
Reply With Quote
 
 
 
 
Chandresh Prakash
Guest
Posts: n/a
 
      11-25-2003
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

(E-Mail Removed) (lonelyplanet999) wrote in message news:<(E-Mail Removed). com>...
> 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?h...edu%26rnum%3D1
>
> 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 ?
>
> HELP! HELP!! HELP!!! HELP!!!! HELP!!!!!

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      11-25-2003
lonelyplanet999 wrote:
> 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...

 
Reply With Quote
 
Brad BARCLAY
Guest
Posts: n/a
 
      11-25-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm looking for html cleaner. Example : convert <h1><span><font>my title</font></span></h1> => <h1>my title</h1>… Stéphane Klein Python 2 03-30-2010 12:35 AM
Re: I'm looking for html cleaner. Example : convert <h1><span><font>my title</font></span></h1> => <h1>my title</h1>… Stefan Behnel Python 0 03-29-2010 08:14 PM
Can span include span? Fulio Open HTML 5 06-26-2009 10:24 PM
Share common HTML code among different HTML files? MK HTML 6 05-15-2005 11:32 PM
DataGrid (body only) contained in <span>...</span> tags Dan Bishop ASP .Net Datagrid Control 2 06-07-2004 04:00 PM



Advertisments