包不存在 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20642382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:18:35  来源:igfitidea点击:

Package does not exists - Java

java

提问by TheWebs

This is a very trivial question, but stack doesn't seem to have very many good answers on it. (see below for the tree structure)

这是一个非常微不足道的问题,但堆栈似乎没有很多好的答案。(树结构见下文)

in start.javaI am doing: import web.*, but it cant find the package web. All the classes in web are either package web.exceptions;or package web;or even package web.interfaces;

start.java我正在做:import web.*,但它无法找到包 web。在网络中的所有类或者是package web.exceptions;或者package web;甚至package web.interfaces;

And I thought for importing packages you do: import path.to.package.name.*;where star imports all classes of that package, or you could do import path.*;which would import all packages and sub packages and their classes.

我想导入包你这样做:import path.to.package.name.*;star 导入该包的所有类,或者你可以这样做import path.*;将导入所有包和子包及其类。

What am I missing?

我错过了什么?

The exact error is:

确切的错误是:

start.java:1: error: package web does not exist

The class in question is:

有问题的类是:

package src; // right package?

import web.*;

public class Start{

    public static void main(String[] args){
        if(args[0].startsWith("--port")){
          String[] argument = args[0].split("=");
          WebServer webServer = new WebServer(argument[1]);
        }
    }
}

Tree Path

树路径

I moved Start.javato web/and changed the import web.*;to import server.*;now it doesn't know where server is, or what it is ...

我感动Start.javaweb/,改变了import web.*;import server.*;现在不知道在哪里服务器,或者它是什么?

.
└── src
    ├── MainClass.txt
    └── web
        ├── Start.java
        └── server
            ├── WebServer.java
            ├── exceptions
            │?? └── WebServerException.java
            └── interfaces
                └── WebServerInterface.java

采纳答案by Anirban Nag 'tintinmj'

"or you could do import path.*; which would import all packages and sub packages and their classes"

“或者你可以做导入路径。*;这将导入所有包和子包及其类”

This is totally wrong. If we say

这是完全错误的。如果我们说

import java.util.*;

we mean that import all classes from java/util/path. If what you are saying is true then we can just say

我们的意思是从java/util/路径导入所有类。如果你说的是真的,那么我们只能说

import java.*;

even

甚至

import *.*;

and it will import all classes under JDK. NO IT'S NOT.

它将导入JDK下的所有类。不,不是



After OP's EDIT

OP 编辑​​后

Start.javais under src/web/folder. So the package statement of Start.javashould be

Start.javasrc/web/文件夹下。所以包的声明Start.java应该是

package web;

Now if you want to use WebServer.javause

现在如果你想使用WebServer.java使用

import web.server.WebServer;

Now if you want to use WebServerException.javause

现在如果你想使用WebServerException.java使用

import web.server.exceptions.WebServerException;

Now if you want to use WebServerInterface.javause

现在如果你想使用WebServerInterface.java使用

import web.server.interfaces.WebServerInterface;

And please read basic tutorial before you write any code.

并且请在编写任何代码之前阅读基本教程。

Read about Packages.

阅读关于