简体   繁体   English

运行 javac 命令时如何指定类路径?

[英]How do you specify the classpath when running the javac command?

I'm trying to import Bar.java into Foo.java , where my directory is structured like this:我正在尝试将Bar.java导入Foo.java ,我的目录结构如下:

.
└─bar
  └─Bar.java
└─foo
  └─Foo.java

So I create Bar.java :所以我创建Bar.java

package bar;
public class Bar {}

and Foo.java :Foo.java

import bar.Bar;
public class Foo {}

I run javac from the foo directory:我从foo目录运行javac

javac Foo.java -cp ../bar

and receive the error:并收到错误:

Foo.java:1: error: package bar does not exist

My understanding was that the -cp option should be used to point towards the required class ( Bar.java ).我的理解是-cp选项应该用于指向所需的 class ( Bar.java )。 Could someone shed some light on what I'm doing wrong?有人可以阐明我做错了什么吗?

Package bar does not exist because the prompt or terminal "working directory" is inside foo. Package bar 不存在,因为提示符或终端“工作目录”在 foo 中。

Easiest, make a folder java2 and put both folders in it and use use the storage folder as "system working directory" for terminal or prompt, However, normal "package" hierarchies compiled together are both in a top level starting directory tree.最简单的是,创建一个文件夹 java2 并将两个文件夹放入其中,并将存储文件夹用作终端或提示符的“系统工作目录”,但是,一起编译的普通“包”层次结构都在顶级起始目录树中。

Eg例如

 /com/foo/ and /com/bar

So your package would look com.foo and com.bar Put the com folder in the java2(simply a name you can use any name for the storage folder)folder. So your package would look com.foo and com.bar Put the com folder in the java2(simply a name you can use any name for the storage folder)folder. Your import statement at the top of the class files should have the com directory added to import the other file. class 文件顶部的导入语句应添加 com 目录以导入其他文件。 Just command inside java2 folder只需在 java2 文件夹中执行命令

javac /com/foo/Foo.java

-cp is for specifying to include dependent.jar libraries for the compilation. -cp 用于指定包含dependent.jar 库进行编译。

If Foo does not import Bar then it will be required to be separately compiled the same way as Foo.如果 Foo 不导入 Bar 则需要像 Foo 一样单独编译。

When framework API libraries are made its the user program requires to mark a starting point to unload the jar or look for a package folder and is traditionally "com", however, it does not distinguish which API until a folder lower so your package folder really should be structured When framework API libraries are made its the user program requires to mark a starting point to unload the jar or look for a package folder and is traditionally "com", however, it does not distinguish which API until a folder lower so your package folder really应该结构化

/com/mypersonalapi/foo /com/mypersonalapi/bar /com/mypersonalapi/foo /com/mypersonalapi/bar

com.mypersonalapi.foo com.mypersonalapi.bar com.mypersonalapi.foo com.mypersonalapi.bar

You need to give like this:你需要像这样给出:

javac Foo.java -cp ..

When you give classpath as shown in your question, it expects the package "bar" under the folder "bar";当您按照问题所示提供类路径时,它需要文件夹“bar”下的 package “bar”; which is not intended.这不是故意的。

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

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