简体   繁体   中英

javac multiple command options

I've sorted my Java files into subdirectories to make it neater and was wondering if it is possible to access jars and source files and then place class files into another specified folder in one javac command?

As standard, I have my jar file in /lib, java files in /src, and an empty folder called /bin for where my class files should go.

When my java & class files were top level and only the jar was in a subdirectory my terminal command to compile was:

javac -cp .:./lib/mysql-connector-java-5.1.38-bin.jar Test.java 

I so far understand that I need -cp to find the jar but would also need -d to specify where I want the class files placed and -sourcepath to find the source files (Wouldn't I just need src/Test.java though?).

How would I be able to make that a legal input for terminal?

A step up question from that would be is there a way to create a script to run that javac command every time in linux? That would be very neat to know.

Many thanks!

First to answer your question

... is possible to access jars and source files and then place class files into another specified folder in one javac command?

Yes. Use -cp to specify the classpath and -d to specify the destination directory. Note that class files will be written subdirectories of the destination directory according to the package name hierarchy.

If you want to compile incrementally, it is a good idea to include the -d directory on the classpath. For a non-incremental (clean) build, remove everything in the -d directory first.

How would I be able to make that a legal input for terminal?

javac -d bin -cp bin:./lib/mysql-connector-java-5.1.38-bin.jar Test.java 

A step up question from that would be is there a way to create a script to run that javac command every time in linux?

Every time what?

  • Yes it is possible to write scripts in Linux. Relatively easy in fact, if you are doing simple things. (I'm sure you could finds some tutorials on shell scripting.)

  • Yes it is possible to write a script to run the javac command.

  • Yes it is possible to write a script to run the javac command and other things ... before or afterwards.

  • Is it a good idea? Up to you to decide.

But there are better ways to build (particularly) large Java applications than writing ad hoc shell scripts. I recommend that you take the time to find out about portable Java build tools such as Ant, Maven and Gradle

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