简体   繁体   English

Java编译,程序包和相对路径

[英]Java Compilation, Packages, and Relative Paths

While using jGRASP I noticed that the output for programs in packages was slightly different than normal, specifically the program names appeared to be relative paths starting from the top of the class. 使用jGRASP时,我注意到程序包中程序的输出与正常输出略有不同,特别是程序名称似乎是从类顶部开始的相对路径。

For example, for a Java program that includes the statement package ch01.stacks; 例如,对于包含语句package ch01.stacks;的Java程序package ch01.stacks; , the compile output looks like: ,编译输出如下所示:

 javac -g ch01\stacks\ArrayStack.java

and the run output appearing similarly. 运行结果显示类似。

I was wondering if there was a relatively straightforward way to simulate this behavior in other programs such as Notepad++ or gedit where users can set up scripts to compile programs. 我想知道是否存在相对简单的方法来在其他程序(例如Notepad ++或gedit)中模拟这种行为,用户可以在其中设置脚本来编译程序。

EDIT: I apologize, I forgot to mention that the compilation scripts I'm talking about are essentially the program filename passed to javac . 编辑:我很抱歉,我忘了提及我所谈论的编译脚本实质上是传递给javac的程序文件名。 I would rather not use absolute paths, I would like my scripts to work in a way similar to jGRASP if at all possible. 我宁愿不使用绝对路径,我希望脚本尽可能以类似于jGRASP的方式工作。

To further clarify the issue at hand, with my current scripts I believe the package structure is giving me issues, since it is compiling in the current directory of the program. 为了进一步阐明当前的问题,我相信使用当前的脚本,程序包结构会给我带来问题,因为它正在程序的当前目录中进行编译。 I am looking for a way to relatively compile my Java programs with respect to package structure. 我正在寻找一种相对于程序包结构相对编译Java程序的方法。

That is, is there any way to detect the top directory needed for the compile ( ch01 in the previous example) without having to dig through the program looking for package ? 也就是说,有什么方法可以检测编译所需的顶层目录(在前面的示例中为ch01 ),而无需在程序中寻找package

I'm still not sure what you want, and this wouldn't fit into a comment. 我仍然不确定您想要什么,因此不适合置评。

jGRASP's behavior is precisely normal behavior. jGRASP的行为恰好是正常行为。 Java class files, both the source, and compiled classes, live in a file hierarchy that mirrors their package names. Java类文件(包括源类和已编译类)都位于反映其包名称的文件层次结构中。 Any Java IDE or command-line build tool ( Ant , Maven , Gradle , whatever) understands this, and behaves accordingly. 任何Java IDE或命令行构建工具( AntMavenGradle等等)都可以理解这一点,并表现出相应的行为。

If you genuinely wish to re-invent those wheels, your code needs to do the same. 如果您真的希望重新发明这些轮子,那么您的代码也需要这样做。 If your Java source seriously doesn't live in the canonical package/file hierarchy, then yes, you'll need to interrogate the source for each file's package declaration, and put the compiled .class file into the appropriate directory. 如果您的Java源代码严重不在规范的包/文件层次结构中,则可以,您需要为每个文件的package声明查询源代码,并将已编译的.class文件放入相应的目录中。

javac 's -d option sets the output directory. javac-d选项设置输出目录。 Compiled classes will be placed in their correct location based on their package name. 编译的类将根据其包名称放置在正确的位置。 However when compiling, all imports must be available on the classpath, whether your own classes, or those of a third-party library (generally in a jar). 但是,在编译时,所有导入都必须在类路径上可用,无论您是自己的类还是第三方库的类(通常在jar中)。

In short: do not do what you're asking about. 简而言之:不要做您想问的事情。 I can think of no good reason to do so. 我认为没有充分的理由这样做。 (This doesn't mean there aren't any good reasons, but I'm... highly skeptical.) It's even possible to use relatively-canned make files to build Java projects; (这并不意味着没有任何充分的理由,但是我……对此表示高度怀疑。)甚至可以使用相对罐头的make文件来构建Java项目。 a better solution that what you're proposing, but still a horrible idea. 更好的解决方案,但仍是一个可怕的主意。

Note: When you run a Java application, the class files must be present in the expected hierarchy, whether packaged in a jar (or war) file, or on the filesystem. 注意: 运行 Java应用程序时,无论打包在jar(或war)文件中还是文件系统中,类文件都必须存在于预期的层次结构中。

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

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