Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

java - 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...