简体   繁体   English

Java-软件包问题

[英]Java - Package woes

I'm not new to Java but I never learned Packages. 我对Java并不陌生,但从未学习过Packages。 Anyways, let's say I create a folder called maina . 无论如何,假设我创建了一个名为maina的文件夹。

I put in file main.java in folder maina : 我把文件main.java文件夹maina

package maina;

import other.Dice;

public class main
{


    public static void main(String[] args)
    {

        System.out.println("Hello world!");
        System.out.println(Dice.test);
    }
}

Then I create a new folder called other inside the folder maina . 然后,我在maina文件夹中创建一个名为other的新文件夹。 In folder other I put file Dice.java : other文件夹中,我放入文件Dice.java

package other;

public class Dice
{

public Dice() {

        String test = "Testing!";
    }

}

OK, now Dice.java compiles fine. 好的,现在Dice.java编译了。 However when I compile main.java I get this error: 但是,当我编译main.java时,出现此错误:

C:\Users\tekaffi\Documents\ask\maina\main.java:13: cannot find symbol
symbol  : variable test
location: class other.Dice
        System.out.println(Dice.test);
                               ^
1 error

Tool completed with exit code 1

What am I doing wrong? 我究竟做错了什么?

Here's the error I get when I compile: 这是我编译时遇到的错误:

C:\Users\wekaffi\Documents\ask\maina\myMain.java:3: package maina.other does not exist
import maina.other.Dice;
                  ^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol  : class Dice
location: class maina.myMain
        Dice myDice = new Dice();
        ^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol  : class Dice
location: class maina.myMain
        Dice myDice = new Dice();
                          ^
3 errors

Tool completed with exit code 1

It has nothing to do with packages. 它与包无关。

Your code is seriously messed up, you're trying to call a "test" member on the "Dice" class but you haven't created that member. 您的代码严重混乱,您试图在“ Dice”类上调用“ test”成员,但尚未创建该成员。 besides that, you can't have a class named "main" and then have a static main method in it beacuse the compiler will think the main method is the constructor you need to rename your class to something else. 除此之外,您不能拥有一个名为“ main”的类,然后再具有一个静态的main方法,因为编译器会认为main方法是您需要将类重命名为其他名称的构造函数

For your code to work your Dice class needs to look like this: 为了使代码正常工作,Dice类需要如下所示:

package maina.other;

public class Dice
{

public String test;

public Dice() {

        this.test = "Testing!";
    }

}

And for the print to work you need to create a new instance of Dice before you print Either that or you make Dice static. 为了使打印工作正常,您需要在打印之前创建一个新的Dice实例,或者将Dice设为静态。 So your main needs to be renamed to myMain and then the class should look like this: 因此,您的主对象需要重命名为myMain,然后该类应如下所示:

package maina;

import maina.other.Dice;

public class myMain
{


    public static void main(String[] args)
    {

        System.out.println("Hello world!");
        Dice myDice = new Dice();
        System.out.println(myDice.test);
    }
}

If you're placing stuff where you said you are, it should work fine package-wise 如果您将内容放置在您所说的位置,则应该在打包时效果很好

Your Dice class must have a package declaration like so 您的Dice类必须具有如下的包声明

package maina.other;

Your main class should import Dice like so 您的主要班级应该像这样导入Dice

import maina.other.*;

如果在/ maina / other中,它将是包maina.other

Dice needs a public static string test . Dice需要public static string test The current test is a non-static local variable to the constructor. 当前测试是构造函数的非静态局部变量。 Or you can make test non-static and then have the constructor set the test member and then do new Dice().test in the main.java 或者,您可以使test非静态的,然后让构造函数设置test成员,然后在main.java执行new Dice().test

And the package name doesn't matter, foldering is only a convention and is IIRC ignored by the compiler. 包名称并不重要,文件夹只是一个约定,编译器会忽略IIRC。 So thats not the issue here! 所以这不是这里的问题!

当您将包“ other”放入“ maina”内部时,新包为

package maina.other;
package maina.other;

As a side note, if you're using an IDE like Eclipse, you don't have to make the directories manually - it does it for you. 附带说明一下,如果您使用的是Eclipse之类的IDE,则无需手动创建目录-它会为您完成。 Also navigating your packages with the package viewer is easy. 使用包查看器浏览包也很容易。

This one is tough to explain, and you've mentioned that you're new to Java, so please don't let me confuse you. 这很难解释,您已经提到您是Java的新手,所以请不要让我感到困惑。

The package of a top-level type, such as main or Dice is whatever is listed in the package declaration. 顶级声明的包(例如mainDice是包声明中列出的任何内容。 The package for Dice could just be other , even though the corresponding directory is nested inside the directory that corresponds to package amain . 即使相应的目录嵌套在与amain软件包相对应的目录中, Dice的软件包也可能是other软件包。

The key is resource discovery when compiling and running. 关键是编译和运行时的资源发现。 When you compile, you can specify a sourcepath and a classpath that helps the compiler resolve dependencies. 编译时,可以指定源路径和类路径,以帮助编译器解决依赖关系。 Likewise, when you run, you can specify a classpath that helps the JVM resolve dependencies. 同样,在运行时,可以指定有助于JVM解决依赖关系的类路径。 There is no restriction that root packages not be nested inside one another. 没有限制,即根包不能相互嵌套。 So, both amain and other could be root packages, like so: 因此, amainother都可以是根软件包,如下所示:

% cd <directory-that-contains-amain>
% javac -sourcepath .:amain amain/main.java amain/other/Dice.java
% java -classpath .:amain amain.main

This is considered abnormal (and consequently poor) practice, however. 但是,这被认为是不正常的(因此效果不佳)。 You shouldn't do it, but you could. 您不应该这样做,但是可以。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM