如何使用javac编译java包结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19382593/
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-12 16:38:38  来源:igfitidea点击:

How to compile java package structures using javac

javacommand-linecompilationpackagejavac

提问by Pswiss87

I am trying to compile (from the command line) a java package that imports another package of my own. I was following a tutorial onlinebut it seems that I get an error when I try to compile the final java file (CallPackage.java).

我正在尝试编译(从命令行)一个导入我自己的另一个包的 java 包。我正在在线学习教程,但是当我尝试编译最终的 java 文件 (CallPackage.java) 时,似乎出现错误。

Here is the file structure:

这是文件结构:

+ test_directory (contains CallPackage.java)
   -> importpackage
       -> subpackage (contains HelloWorld.java)

Here is CallPackage.java:

这是 CallPackage.java:

/// CallPackage.java
import importpackage.subpackage.*;
class CallPackage{
  public static void main(String[] args){
  HelloWorld h2=new HelloWorld();
  h2.show();
  }
}

and here is HelloWorld.java:

这是HelloWorld.java:

///HelloWorld.java

package importpackage.subpackage;

public class HelloWorld {
  public void show(){
  System.out.println("This is the function of the class HelloWorld!!");
  }
}

Attempted Steps

尝试的步骤

  1. Go to the subpackage and compile HelloWorld.java with $javac HelloWorld.java.
  2. Go to test_directory and compile CallPackage.java with $javac CallPackage.java.
  1. 转到子包并使用$javac HelloWorld.java.
  2. 转到 test_directory 并使用$javac CallPackage.java.

This gives me an error on the last command:

这给了我最后一个命令的错误:

CallPackage.java:1: package importpackage.subpackage does not exist
import importpackage.subpackage.*;
^
CallPackage.java:4: cannot find symbol
symbol  : class HelloWorld
location: class CallPackage
  HelloWorld h2=new HelloWorld();
  ^
CallPackage.java:4: cannot find symbol
symbol  : class HelloWorld
location: class CallPackage
  HelloWorld h2=new HelloWorld();
                    ^
3 errors

How can I compile both packages? Thanks so much for any help!

如何编译这两个包?非常感谢您的帮助!

采纳答案by Jazzy Josh

Are you sure importpackage/subpackage is in your classpath?

你确定 importpackage/subpackage 在你的类路径中吗?

-cp path or -classpath path

Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

If the -sourcepath option is not specified, the user class path is also searched for source files.

If the -processorpath option is not specified, the class path is also searched for annotation processors.

-cp 路径或 -classpath 路径

指定在哪里可以找到用户类文件,以及(可选)注释处理器和源文件。该类路径覆盖 CLASSPATH 环境变量中的用户类路径。如果 CLASSPATH、-cp 和 -classpath 均未指定,则用户类路径由当前目录组成。有关更多详细信息,请参阅设置类路径。

如果未指定 -sourcepath 选项,还会在用户类路径中搜索源文件。

如果未指定 -processorpath 选项,则还会在类路径中搜索注释处理器。

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

回答by Dev

(1)first compile the code

(1)首先编译代码

javac -d importpackage.subpackage.HelloWorld

javac -d importpackage.subpackage.HelloWorld

(2) and then compile the CallPackage.java

(2)然后编译CallPackage.java

javac CallPackage.java

javac CallPackage.java

回答by Pswiss87

The issue was that the class path needs to be set for each command (javac and java):

问题是需要为每个命令(javac 和 java)设置类路径:

Attempted Steps

尝试的步骤

  1. instead of going to subpackage, compile HelloWorld.java from the top_level:

    $javac -cp . importpackage/subpackage/HelloWorld.java

  2. compile CallPackage.java in the same way:

    $javac -cp . CallPackage.java

  3. run the file using the class path also:

    $java -cp . CallPackage

  1. 从 top_level 编译 HelloWorld.java,而不是去分包:

    $javac -cp . importpackage/subpackage/HelloWorld.java

  2. 以同样的方式编译 CallPackage.java:

    $javac -cp . CallPackage.java

  3. 还使用类路径运行文件:

    $java -cp . CallPackage

NOTE: running "$java CallPackage" will give an error "Error: Could not find or load main class CallPackage"

注意:运行“$java CallPackage”将给出错误“错误:无法找到或加载主类 CallPackage”

In summary, during each step, the class path must be specified. It worked after running it as such.

总之,在每个步骤中,必须指定类路径。它在运行后工作。

回答by V?nh Th?y Tr?n

Same situation to me. And I came to take over it by compiling classes at the same time.
For example, here is my project:

和我一样的情况。我通过同时编译类来接管它。
例如,这是我的项目:

+ beerV1
   -> classes
   -> src
         -> com
              -> example
                   -> model
                        -> BeerExpert.java
                   -> web
                        -> BeerSelect.java


BeerExpert.java:


啤酒专家.java:

package com.example.model;
import ...

public class BeerExpert{
    ...
}


BeerSelect.java:


啤酒选择.java:

package com.example.web;
import com.example.model.*;
import ...

public class BeerSelect {
      ...
}


As you can see: BeerSelect.javais trying to import classes in com.example.modelpackage.
At the first time, I compiled BeerExert.javafirst by command:

--> javac -d classes src/com/example/model/BeerExpert.java

Then:
--> javac -d classes src/com/example/web/BeerSelect.java

And the result was:
-->... error: package com.example.model does not exist


如您所见:BeerSelect.java正在尝试导入com.example.model包中的类。
第一次,我先通过命令编译BeerExert.java

--> javac -d classes src/com/example/model/BeerExpert.java

然后:
--> javac -d classes src/com/example/web/BeerSelect.java

结果是:
-->... error: package com.example.model does not exist

So, I knew that compiling multiple classes separatelywill not work in this case.

所以,我知道在这种情况下单独编译多个类是行不通的。


After suffering on google, I found this very simple way to solve the problem:
Just compile all at once:


在google上苦苦挣扎后,我发现了一个非常简单的解决问题的方法:
只需一次编译:

--> javac -d classes src/com/example/model/BeerExpert.java src/com/example/web/BeerSelect.java 


Finally, here is what I got:


最后,这是我得到的:

 + beerV1
           -> classes
                 -> com
                      -> example
                           -> model
                                -> BeerExpert.class
                           -> web
                                -> BeerSelect.class
           -> src
                 -> com
                      -> example
                           -> model
                                -> BeerExpert.java
                           -> web
                                -> BeerSelect.java

Hope that helps.

希望有帮助。