如何使用 javac 编译 java 包结构

时间:2023-05-05
本文介绍了如何使用 javac 编译 java 包结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试编译(从命令行)一个导入我自己的另一个包的 java 包.我正在关注 tutorial online 但似乎在我尝试编译最终的 java 文件(CallPackage.java).

I am trying to compile (from the command line) a java package that imports another package of my own. 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).

这是文件结构:

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

这里是 CallPackage.java:

Here is CallPackage.java:

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

这里是 HelloWorld.java:

and here is HelloWorld.java:

///HelloWorld.java

package importpackage.subpackage;

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

尝试的步骤

  1. 进入子包,用$javac HelloWorld.java编译HelloWorld.java.
  2. 转到 test_directory 并使用 $javac CallPackage.java 编译 CallPackage.java.
  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.

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

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!

推荐答案

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

Are you sure importpackage/subpackage is in your classpath?

-cp 路径或-classpath 路径

-cp path or -classpath path

指定在哪里可以找到用户类文件,以及(可选)注释处理器和源文件.此类路径覆盖 CLASSPATH 环境变量中的用户类路径.如果既未指定 CLASSPATH、-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. 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.

如果没有指定-sourcepath选项,也会在用户类路径中搜索源文件.

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

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

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

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

这篇关于如何使用 javac 编译 java 包结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:您在 Java 项目中使用什么策略来命名包,为什么? 下一篇:编译时如何通配符包含 JAR 文件?

相关文章