使用 javac 编译来自不同目录的文件,引用依赖的 jar 文件? [英] compile files from different directories with javac, referring a depending jar file?

查看:47
本文介绍了使用 javac 编译来自不同目录的文件,引用依赖的 jar 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

我有 4 个包裹:

  • root/src/terminal - 有一些 java 文件
  • root/src/mail - 有一些 java 文件
  • root/src/data - 有一些 java 文件
  • root/src/main - 有一个 java 文件 Main.java

我还有以下文件

  • root/bin - 用于存储 .class 文件的文件夹
  • root/mail.jar - 一个 jar 文件,其中包含在我的代码中使用的重要类

在 root 中,我想输入一个终端命令,它编译 root/src/main/Main.java 并将类文件放在 root/bin 位置.

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.

有人可以告诉我执行此操作的命令吗?我使用的是 Mac(运行 Leopard).

Can someone show me the command to do this? I'm on a Mac (running Leopard).

推荐答案

这里是一个班轮:

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

在提到 Ant 时,您说我宁愿保持简单.".

In reference to Ant you said "I would rather keep it simple.".

事实上,从长远来看,创建一个简单的 Ant build.xml 文件更简单.另一种选择是一堆不可移植的脚本或批处理文件......或大量输入.

In fact in the long term it is simpler to create a simple Ant build.xml file. The alternative is a bunch of non-portable scripts or batch file ... or lots of typing.

要运行应用程序,假设您仍在/xyz/root 目录中:

To run the application, assuming that you are still in the /xyz/root directory:

java -classpath bin:mail.jar main.Main

或在 Windows 上:

Or on Windows:

java -classpath bin;mail.jar main.Main

或者修改上面的在classpath参数中使用绝对路径名;例如

Or modify the above to use absolute pathnames in the classpath argument; e.g.

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

这篇关于使用 javac 编译来自不同目录的文件,引用依赖的 jar 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆