Easy Java command line compiles

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

Personally, I think javac should be smart enough to handle

javac * 

when in a folder only under the root of the package hierarchy. Is there anything in this soccer field?

Edit: the structure of the source folders is complex and the class is not one, so some of the ideas mentioned will not work yet. Thanks at least! Think of 9 levels with code on many levels.

+4
source share
4 answers

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

 javac -classpath . app/Main.java 

must generate the correct class definitions. The compiler will parse the dependencies and compile any other classes. Class files will be displayed in the same directory as the source files.

If, as you say, you have a "more than one record" class, you will need to at least identify all the top-level classes from the dependency hierarchy, which can be specified as additional parameters for javac, indicating the packages as they occur. Thus, assuming you also need to start with other.Entry

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

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

+6
source

Provide them with an Ant script that builds with the correct libraries in the classpath, etc. The script can also perform other tasks, such as creating a JAR, etc.

This requires this user to download and install Ant, but it is not difficult. (And there is nothing that would stop you from providing them with the appropriate Ant distribution kit for installation. Or even send them a ZIP distribution file that has a copy of Ant "preinstalled" in the tree.)

Providing an Ant script means that you avoid being trapped by Java beginners, for example, you forget to set the class path while in the wrong directory, forget to recompile dependent files, and so on. In addition, he is more "professional."

+5
source

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

If BaseProgram refers to Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, they will also be compiled. Similarly, if they are in a package and the package directory is available, they will be compiled.

+3
source

You can create a file that lists 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 parameters and another for the source file - names: (Note that the following lists do not continue the line of 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, for example.

 javac greetings/*.java 

(Again from man javac )

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

+2
source

Source: https://habr.com/ru/post/1304213/


All Articles