简体   繁体   中英

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

I have the following set up:

I have 4 packages:

  • root/src/terminal - has some java files
  • root/src/mail - has some java files
  • root/src/data - has some java files
  • root/src/main - has a single java file, Main.java

I also have the following files

  • root/bin - a folder to store .class files
  • root/mail.jar - a jar file which has important classes used in my code

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.

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

Without knowing your operating system?

What you should look into is using 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.

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; and package main; .

See this: Options in javac command

Or use Apache Ant as suggested by maple_shaft.


From comment give by @maple_shaft:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon.

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." .

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.


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

java -classpath bin:mail.jar main.Main

Or on Windows:

java -classpath bin;mail.jar main.Main

Or modify the above to use absolute pathnames in the classpath argument; eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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