简体   繁体   English

如何使用javac编译java包结构

[英]How to compile java package structures using javac

I am trying to compile (from the command line) a java package that imports another package of my own.我正在尝试编译(从命令行)一个导入我自己的另一个包的 java 包。 I was following a tutorial online but it seems that I get an error when I try to compile the final java file (CallPackage.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 .转到子包并使用$javac HelloWorld.java
  2. Go to test_directory and compile CallPackage.java with $javac CallPackage.java .转到 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!非常感谢您的帮助!

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:从 top_level 编译 HelloWorld.java,而不是去分包:

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

  2. compile CallPackage.java in the same way:以同样的方式编译 CallPackage.java:

    $javac -cp . CallPackage.java

  3. run the file using the class path also:还使用类路径运行文件:

    $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.它在这样运行后工作。

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.java is trying to import classes in com.example.model package.如您所见: BeerSelect.java正在尝试导入com.example.model包中的类。
At the first time, I compiled BeerExert.java first by command:第一次,我先通过命令编译BeerExert.java

--> 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

So, I knew that compiling multiple classes separately will not work in this case.所以,我知道在这种情况下单独编译多个类是行不通的。


After suffering on google, I found this very simple way to solve the problem:在谷歌上苦苦挣扎后,我发现了一个非常简单的方法来解决这个问题:
Just compile all at once:只需一次编译:

--> 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.希望有帮助。

Are you sure importpackage/subpackage is in your classpath?你确定 importpackage/subpackage 在你的类路径中吗?

-cp path or -classpath path -cp 路径或 -classpath 路径

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.该类路径覆盖 CLASSPATH 环境变量中的用户类路径。 If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.如果 CLASSPATH、-cp 和 -classpath 均未指定,则用户类路径由当前目录组成。 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.如果未指定 -sourcepath 选项,还会在用户类路径中搜索源文件。

If the -processorpath option is not specified, the class path is also searched for annotation processors.如果未指定 -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

It's an old topic but Google has led me to this site.这是一个古老的话题,但谷歌把我带到了这个网站。 For the completness, I'd like to add one bit to @Vĩnh Thụy Trần's answer on how to run the main class after compiling it into a custom folder.为了完整起见,我想在@Vĩnh Thụy Trần 关于如何在将主类编译到自定义文件夹后运行它的答案中添加一点。 While it looks trivial now it took me some time to get it right.虽然现在看起来微不足道,但我花了一些时间才把它做好。

In order to run your project, you also need to specify a path to the classes:为了运行您的项目,您还需要指定类的路径:

java -classpath <directory> your.package.name.classname

or或者

java -cp <directory> your.package.name.classname

Taking Vĩnh Thụy Trần's example again, the command would look like this:再次以 Vĩnh Thụy Trần 的示例为例,该命令将如下所示:

java -cp classes com.example.web.BeerSelect

I hope it will help someone as I spent some time figuring it out.我希望它会帮助某人,因为我花了一些时间来弄清楚。

(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

delete your package folder (after pasting you code to some other folder) and then locate to the folder in cmd where you current code is and try javac -d .删除您的包文件夹(将您的代码粘贴到其他文件夹后),然后在 cmd 中找到您当前代码所在的文件夹并尝试 javac -d 。 Helloworld.java (this will create the Helloworldclass and subpackage as well) and try same for you mainfunction code ie Callpackage.java after compiling to run the code try java Callpackage Helloworld.java(这也将创建Helloworldclass 和子包)并在编译运行代码后尝试为您的mainfunction 代码即Callpackage.java 尝试相同的代码try java Callpackage

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

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