8

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?

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

1
  • 3
    If they can't use Java, who not just give them the already-compiled version? Commented Mar 15, 2010 at 23:37

5 Answers 5

13

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

I've found this to apparently not be the case -- at least, depending on certain details. With javac -classpath ., I think it might be, though I'm still working through things on the source I got. But with just javac Whatever.java, it's getting lots of error: cannot find symbol type errors, even though the referenced symbol has a .java file in the same directory.
5

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

Comments

2

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?

Comments

0

A simple way would be by using:

javac *.java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.