简体   繁体   English

使用javac从不同目录编译文件,引用依赖的jar文件?

[英]compile files from different directories with javac, referring a depending jar file?

I have the following set up: 我有以下设置:

I have 4 packages: 我有4个软件包:

  • root/src/terminal - has some java files root / src / terminal-有一些Java文件
  • root/src/mail - has some java files root / src / mail-有一些Java文件
  • root/src/data - has some java files root / src / data-有一些Java文件
  • root/src/main - has a single java file, Main.java root / src / main-具有单个Java文件Main.java

I also have the following files 我也有以下文件

  • root/bin - a folder to store .class files root / bin-用于存储.class文件的文件夹
  • root/mail.jar - a jar file which has important classes used in my code root / mail.jar-一个jar文件,其中包含在我的代码中使用的重要类

Within the root, I would like to enter a terminal command which compiles root/src/main/Main.java and puts the class files in the root/bin location. 在根目录下,我想输入一个终端命令,该命令编译root / src / main / Main.java并将类文件放在root / bin位置。

Can someone show me the command to do this? 有人可以告诉我执行此操作的命令吗? I'm on a Mac (running Leopard). 我在Mac(正在运行Leopard)上。

Without knowing your operating system? 不知道您的操作系统?

What you should look into is using Apache Ant. 您应该研究的是使用Apache Ant。 It is a build tool that once installed and configured can utilize a build.xml file in your root to compile class files to a folder as well as package a jar file. 它是一个构建工具,一旦安装和配置,它就可以利用根目录中的build.xml文件将类文件编译到文件夹中并打包jar文件。

http://ant.apache.org/ http://ant.apache.org/

try this: 尝试这个:

javac -cp "/root/mail.jar;/root/src;" -d "/root/bin" Main.java

This is written hoping that you have package declarations in your classes from src folder like package terminal; 编写此代码是希望您在src文件夹中的类(如package terminal;具有程序包声明package terminal; and package main; package main; .

See this: Options in javac command 看到这个: javac命令中的选项

Or use Apache Ant as suggested by maple_shaft. 或使用maple_shaft建议的Apache Ant。


From comment give by @maple_shaft: 来自@maple_shaft的评论:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon. 在Unix,Linux操作系统中,类路径分隔符是冒号而不是分号。

Here's the one liner: 这是一个班轮:

cd /xyz/root
rm -rf bin/*
javac -d bin -classpath mail.jar -sourcepath src main/Main.java

Alternatively, you could use absolute directory names: 另外,您可以使用绝对目录名称:

rm -rf /xyz/root/bin/*
javac -d /xyz/root/bin -classpath /xyz/root/mail.jar \
      -sourcepath /xyz/root/src /xyz/root/ main/Main.java

In reference to Ant you said "I would rather keep it simple." 在提到Ant时,您说过“我宁愿保持简单。” .

In fact in the long term it is simpler to create a simple Ant build.xml file. 实际上,从长远来看,创建一个简单的Ant build.xml文件会更简单。 The alternative is a bunch of non-portable scripts or batch file ... or lots of typing. 替代方案是一堆不可移植的脚本或批处理文件……或大量键入。


To run the application, assuming that you are still in the /xyz/root directory: 要运行该应用程序,假设您仍在/xyz/root目录中:

java -classpath bin:mail.jar main.Main

Or on Windows: 或在Windows上:

java -classpath bin;mail.jar main.Main

Or modify the above to use absolute pathnames in the classpath argument; 或修改以上内容以在classpath参数中使用绝对路径名; eg 例如

java -classpath /xyz/root/bin:/xyz/root/mail.jar main.Main

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

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