在命令行中从不同的程序包访问类 [英] Accessing class from different package in command line
问题描述
我在C:\ Users \ Documents文件夹中创建了两个文件夹.我将文件夹命名为A和B.在文件夹A内,我在Java类下面编写了代码.
I have created two folders in C:\Users\Documents folder. I have named the folders as A and B. Inside folder A, I have written below java class.
package A;
public class Food {
int a =6;
public int c = 10;
}
在文件夹B中,我写了下面的课,
Inside folder B, I have below class written,
package B;
import A.*;
public class Car {
public static void main(String[] args) {
Food food = new Food();
System.out.println(food.c);
}
}
我能够从文件夹A内编译Food类.但是,当我尝试从文件夹B内编译Car类时,出现以下编译错误.该如何解决?
I am able to compile class Food from inside folder A. But when I am trying to compile class Car from inside folder B, I am getting below compilation error. How to resolve this?
Car.java:2: error: package A does not exist
import A.*;
^
Car.java:6: error: cannot find symbol
Food food = new Food();
^
symbol: class Food
location: class Car
Car.java:6: error: cannot find symbol
Food food = new Food();
^
symbol: class Food
location: class Car
3 errors
推荐答案
您应该位于 Documents
文件夹中,才能同时访问A和amp;B,同时编译 Car
类.
You should be in the Documents
folder to get access both the packages A & B while compiling Car
class.
您的编译语句必须类似于
And your compile statement must be something like
javac -cp . B/Car.java
注意:考虑到.class文件在下面,我将类路径作为当前目录(.).
要运行 Car
类,请使用以下命令.
To run the class Car
use the below command.
java -cp . B.Car
使用main()运行Class时,您需要给出类名的完整限定路径,即 packagename.className
While running Class with main(), you need to give full qualified path of class name i.e. packagename.className
这篇关于在命令行中从不同的程序包访问类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!