开发者

java classpath related query with import statement

开发者 https://www.devze.com 2023-02-04 17:02 出处:网络
My program below is called Apple.java import food.Fruit; class Apple extends Fruit{ public static void main(String[] args){

My program below is called Apple.java

import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
    int y=20,z=0;
    z=y+10;
    System.out.println("x+y is "+z);
}
}

This is another program called Fruit.java

 package food;
 public abstract class Fruit{
    public static void main(String[] args){
    int x=10;
    System.out.println("x is "+x);
    }
 }

Both these programs are stored in the following directory path :- G:\Java\food I did successfully compile both the programs using the following command:-

G:\>javac Java\food\*.java

and it produced class files. I did manage to execute the Fruit program using the following command:-

开发者_StackOverflow中文版G:\>java -classpath G:\Java food.Fruit

However, when I tried to do the same for the Apple program it resulted in error messages.

G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)

Could someone please help me in figuring out what the error is? I am trying to get myself ready for the SCJP cert exam and hence I reference this example back to the McGrawHill publication book written by Kathy Sierra and Bert Bates.


Edit #2

Yes, I did because that was how the code was in the book. Is there a way I could still execute the file without having to include the package statement.

This is what the question looks like:

  1. Attempt to compile the two files. If you want to use the Apple class, make sure you place the Fruit.class file in the food subdirectory.

Edit #3

I did try that and got the following errors

G:\>java -classpath G:\Java\food Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Fruit
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    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)
Caused by: java.lang.ClassNotFoundException: food.Fruit
    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)
    ... 12 more
Could not find the main class: Apple.  Program will exit.

Edit #4

I did manage to execute it without having to include the statement package food;

G:\>java -classpath G:\Java\food;G:\Java Apple
x+y is 30

What I changed was I moved the Apple.class file into a separate folder which makes sense because the folder "food" is an indication of the package and as Darvis pointed all files in the folder needed to have the package name else it would not execute.


Edit #5 This worked too

G:\>java -classpath G:\Java Apple
x+y is 30


because you haven't specified food package for that. make it like this and it will work

package food;
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
    int y=20,z=0;
    z=y+10;
    System.out.println("x+y is "+z);
}
}

Update:

When you execute below

G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)

it will add G:\Java in class path from there it will relatively search for class apple in food DIR with package name specified food

Or. if you don't want to specify package in the class then

G:\>java -classpath G:\Java\food Apple
0

精彩评论

暂无评论...
验证码 换一张
取 消