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

L

lonelyplanet999

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!!!!!
 
T

Thomas Weidenfeller

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

--
 
C

Chandresh Prakash

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

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!!!!!
 
S

Sudsy

lonelyplanet999 said:
: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...
 
B

Brad BARCLAY

lonelyplanet999 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top