0

I'm trying to compile a Java project from the command line. The project contains class files in different packages. The program compiles and run fine if I specify every java file of every package. Here's my directory structure:

toplevel/
    mainFile.java
    level1/ (Contains fileA.java)

Now if I do javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java, this compiles fine and I can run it with java toplevel/mainFile. Now if a create a new folder called 'level2' in level1 and create a class 'B' inside it, the new directory structure becomes:

toplevel/
    mainFile.java
    level1/ (Contains fileA.java)
        level2/ (Contains fileB.java)

To compile this I have to do: javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java toplevel/level1/level2/fileB.java which is becoming ridiculous. Is there an instruction that recursively compiles each package and the files inside it?

df611
  • 143
  • 11
  • 1
    Why not use a build tool like ant or maven? It will be trivial with those. – rocketboy Aug 28 '13 at 15:18
  • 1
    Possible repeat of [javac option to compile recursively](http://stackoverflow.com/questions/6623161/javac-option-to-compile-recursively) – Chris B Aug 28 '13 at 16:23
  • @rocketboy I don't want to use any build tool this requires learning some extra stuff. The user has to be able to run it from the command line – df611 Aug 28 '13 at 16:52

1 Answers1

1

Have you tried -sourcepath flag. From the Oracle documentation,

-sourcepath sourcepath Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.

Shiva Kumar
  • 2,991
  • 1
  • 22
  • 31