• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to execute a file xi.java from package ?  RSS feed

 
Tanka Banerjee
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I have two files xi.java and yi.java . xi.java is inside package X and yi.java is inside package Y. package Y is inside package X.
Code xi.java :
package X;
import Y.*;
public class xi {
public static void main(String[] args) {
byte[] bytes = new byte[256];
X.Y.yi.process(bytes);
}
}


Code yi.java :

package Y;
import X.*;
public class yi {
public static void process(byte[] nn) {
System.out.println("Inside method byte, running whole night");
}
}

I am entering into package X by cd command and Compiling xi.java . Fine.
But when i am executing
D:\X>java X.xi
Exception in thread "main" java.lang.NoClassDefFoundError: X/xi

[ August 17, 2006: Message edited by: Tanka Banerjee ]
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which order did you compile the two classes in. If compiling classes from a command prompt, one has to compile the "supplier" class (here Y) before the "client" (here X).

[ August 17, 2006: Message edited by: Campbell Ritchie ]
[ August 17, 2006: Message edited by: Campbell Ritchie ]
 
Tanka Banerjee
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it ok. But i am getting runtime error.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A:\>javac -d . Yi.java
Yi.java:3: package X does not exist
import X.*;
^
1 error

A:\>javac -d . Xi.java
Xi.java:3: package Y does not exist
import Y.*;
^
Xi.java:10: cannot find symbol
symbol : variable Yi
location: class X.Xi
Yi.process(bytes);
^
2 errors
[break]Here I deleted the import x; statement from Yi[/break]
A:\>javac -d . Yi.java

A:\>javac -d . Xi.java

A:\>java X.Xi
Inside method byte, running whole night
I put your two classes on my floppy drive, and the above is what I got. It won't compile Xi because it can't find the Y package to import (you are using the wrong conventions for package and class names, by the way).
It won't compile Yi because it can't find the X package.
I deleted the import x; statement from Yi, and it worked.

You said you are getting a run time error. You will have to tell us whjat sort of error.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never noticed you put one package inside the other. You really are making things confusing for yourself aren't you?

Your package declarations are wrong; you don't have a package y, but a package x.y.
 
Tanka Banerjee
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved.

D:\>javac -classpath . X\*.java X\Y\*.java

D:\>java -classpath . X.xi
Inside method byte

[ August 17, 2006: Message edited by: Tanka Banerjee ]
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\user>a:

A:\>javac -d . Yi.java

A:\>javac -d . Xi.java
Xi.java:3: package Y does not exist
import Y.*;
^
Xi.java:10: cannot find symbol
symbol : variable Yi
location: class X.Xi
Yi.process(bytes);
^
2 errors
[break]Change the import declaration in Xi to read "import X.Y.*;"[/break]
A:\>javac -d . xi.java

A:\>java x.Xi
Exception in thread "main" java.lang.NoClassDefFoundError: x/Xi (wrong name: X/X
i)
[error]I used x instead of X[/error]

at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown 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)

A:\>java X.Xi
Inside method byte, running whole night

A:\>
It works. You just need to get your import statements right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!