开发者

Easy command line Java compile

开发者 https://www.devze.com 2022-12-23 04:32 出处:网络
So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not req

So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not require writing out lists of the files?

Pers开发者_如何学编程onally I think javac should be smart enough to handle

javac *

when in the folder just under the root of the package hierarchy. Is there anything in this ballpark?

Edit: The source folder structure is complex and the is no single entry class so some of the ideas mentioned so far won't work. Thanks though! Think 9 levels deep with code on many levels.


From the folder that represents the base of your package hierarchy, assuming your entry point class is called Main, in a package called app,

javac -classpath . app/Main.java

should generate the correct class definitions. The compiler will ferret out the dependencies and compile whatever other classes are needed. The class files will appear in the same directory as their source files.

If, as you say, you have 'more than one entry' class, you will have to at least identify all those top level classes from the dependency hierarchy, which can be listed as further params to javac, specifying the packages as they occur. Like so, assuming you also need to start with other.Entry

javac -classpath . app/Main.java other/Entry.java 

Note that you will still have to figure out which of your classes are tops of independent dependency hierarchies, whether you are creating an ant script or doing it this way.


javac BaseProgram.java will compile BaseProgram.java from the current directory, and all classes it references that are available in source code, in the same directory tree.

If BaseProgram references Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, then they too will get compiled. Likewise if they are in a package, and the package directory is available, they will be compiled.


Provide them with an Ant script that does the build with the correct libraries on the classpath, etc. The script can also do other tasks such as building JARs, etc.

This requires that that person downloads and installs Ant, but that is not hard. (And there is nothing to stop you from providing them with an appropriate Ant distro to install. Or even sending them a distro ZIP file that has a copy of Ant "preinstalled" in the tree.)

Providing an Ant script means that you avoid them falling into Java newbie traps such as forgetting to set the classpath, being in the wrong directory, forgetting to recompile dependent files and so on. Plus, it is more "professional".


You can build a file listing all the classes you want to compile (extracted from the javac man page) -

Example - Two Arg Files You can create two argument files -- one for the javac options and the other for the source file- names: (Notice the following lists have no line-continuation characters.)

   Create a file named options containing:

          -d classes
          -g
          -sourcepath /java/pubs/ws/1.3/src/share/classes

   Create a file named classes containing:

          MyClass1.java
          MyClass2.java
          MyClass3.java

   You would then run javac with:

          % javac @options @classes

Or you can use *.java on the command line e.g.

javac greetings/*.java

(Again from man javac)

Or why don't you just compile the source into a jar that your customer can run using their JRE - especially considering they are not Java savvy?


A simple way would be by using:

javac *.java.
0

精彩评论

暂无评论...
验证码 换一张
取 消