简体   繁体   English

在java中使用包内的主类

[英]Using main class inside the package in java

I am trying to to run the file Demo.java which is calling Protection class within the same package but it is giving error This is the main class.我试图运行在同一个包中调用保护类的文件 Demo.java,但它给出了错误这是主类。

package p1;
// Instantiate the various classes in p1.
class Demo {
  public static void main(String args[]) {
    Protection ob1 = new Protection();
    //Derived ob2 = new Derived();
    //SamePackage ob3 = new SamePackage();
  }
}

And this is the class that I want to use in the main class.这是我想在主类中使用的类。

package p1;

public class Protection {

  public int n = 1;
  private int n_pri = 2;
  protected int n_pro = 3;
  public int n_pub = 4;

  public Protection() {
    System.out.println("base constructor");
    System.out.println("n = " + n);
    System.out.println("n_pri = " + n_pri);
    System.out.println("n_pro = " + n_pro);
    System.out.println("n_pub = " + n_pub);
  }
}

It is giving this error:它给出了这个错误:

$ javac Demo.java
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
  symbol:   class Protection
  location: class Demo
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
                     ^
  symbol:   class Protection
  location: class Demo
2 errors
error: compilation failed

You should use javac , not java only您应该使用javac ,而不仅仅是java

When you use the command java , you can execute a file, but only the classes in that file.当您使用命令java ,您可以执行一个文件,但只能执行该文件中的类。 Here you have several files, so you should compile them in order to use them.这里有几个文件,所以你应该编译它们以便使用它们。

Do the following:请执行下列操作:

$ mkdir p1
$ mv Demo.java Protection.java p1/
# edit p1/Demo.java to change `class Demo` to `public class Demo`
$ javac p1/*
$ java p1.Demo

This worked and resulted in the following:这有效并导致以下结果:

base constructor
n = 1
n_pri = 2
n_pro = 3
n_pub = 4

You could try this:你可以试试这个:

  1. open cmd in p1, use javac .\\Demo.java .\\Protection.java ;在 p1 中打开 cmd,使用javac .\\Demo.java .\\Protection.java then you can see two .class files generated然后你可以看到生成了两个 .class 文件
  2. use cd .. , then you can see your package p1使用cd .. ,然后你可以看到你的包p1
  3. use java p1.Demo then you can see the expected output使用java p1.Demo然后你可以看到预期的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在包中找到或加载主类 - Cannot find or load main class inside a package 在另一个包的主包中使用一个包中的java类(同一项目) - Using java class from one package in main of another package (Same project) main()方法的Java类的包约定 - package convention for main() method`s Java class Java-在另一个类中使用包的main方法 - Java - Use main method of a package in another class java无法使用-cp查找或加载主类。 package.class - java could not find or load main class, using -cp . package.class 在主类中使用包名称,导致NoClassFoundException - using Package name in main Class, causes NoClassFoundException 在Java中从该类的主要内部实例化对象 - Instantiating object from inside the main of that class in Java 使用 {mvn clean package} 构建项目后运行 Java 时无法找到或加载主 class - Could not find or load main class when running Java after a project is built using {mvn clean package} 在包/文件夹中运行 Java 程序时,“代码运行器”扩展显示“找不到或加载主类”。 (VS代码) - 'Code Runner' extension shows "Could not find or load main class" when running Java programs inside a package/folder. (VScode) 如何在Java中从包内部引用类 - How to refer to class from inside package in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM