1

I'm trying to compile multiple .java programs from different directories either on Windows, Mac, or Linux... e.g. cmd or terminal. It doesn't matter.

However, I'm sure that many of you are familiar with how Netbeans stores files in different folders. I have been putting different concepts into different folders, and now I want to run all of them.

For example, a chess program that I have looks sort of like this:

/Chess
    /build
        /classes
            /chess
                /Chess.class
            /color
                /colorHelper.class
            /game
                /Board.class
                /Game.class
                /GameManager.class
                /Player.class
            ... etc. (the rest of the directories with .class files)
    /build.xml
    /gameLog.txt
    /manifest.mf
    /nbproject
        ... (some xml and .properties files)
    /src
        /chess
            /Chess.java
        /color
            /ColorHelper.java
        /game
            /Board.java
            /Game.java
            /GameManager.java
            /Player.java
        ... etc. (the rest of the directories with .java files)

So, my question is, how can you use javac *.java (in /src probably) to compile all of the files (because otherwise I get a cannot find symbol error. Since I get a file not found when I run javac *.java in src, I am at a loss.

Thanks in advance,

Dylan

dyl
  • 13
  • 1
  • 1
  • 4
  • 1
    Would you consider in using a program like ANT or Maven or some other tool to compile your classes on the command line? – Jorge Campos Jul 20 '14 at 04:46
  • 2
    @JorgeCampos's right, but if you insist on not doing this, `javac $(find . -name '*.java')` – David Ehrmann Jul 20 '14 at 04:47
  • I've never used those because I've never had to... I could do some research on them. They would allow me to do what I want? (Of course I really could just run the program from Netbeans IDE ... or even put everything into a .jar, then run it with a .bat file ... but I want to learn from cmd or terminal). – dyl Jul 20 '14 at 04:48
  • As you are asking to do this on `Windows, Mac, or Linux` different plattaforms you will have to make a some bash/bat script for each or use one of the tools that i mentioned, it will work in any plattaform if they are proper installed. – Jorge Campos Jul 20 '14 at 04:51
  • right now it's in Mac terminal... in future it will be on windows cmd.exe as well as Ubuntu terminal. :) – dyl Jul 20 '14 at 04:55
  • haha. Sorry; for some reason I thought that was a question. (It's a little late...) Thanks, though – dyl Jul 20 '14 at 05:09

4 Answers4

2

It won't compile in one go.Actually,your src directory doesn't contain any .java file,SO it won't be done in that way!

I am afraid that you'll have to do it by changing your path under src folder to do the same.

You'll have to perform for each chess,color,game,etc. directories to achieve the same.

So,change path at each run or go as advised in the comments to achieve compilation of all the java files.

OR

As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java').

It will compile all .java files under your present directory(src(.)).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

javac *.java compiles all the Java files within the current directory. In your case, since you have different hierarchy of folders based on the package, you cannot achieve this in one go. At best you can run the command javac *.java on each folder.

Inder
  • 160
  • 9
0

You should try using an automated build system like maven or ant. It will allow you to build your project with a single command. It will even allow running unit tests, and packaging your application as a jar (or whatever package you prefer).

If you really don't want to use one of those you can still do it with a single (albeit lengthy) command. The *.java is just a wildcard in a file path. Its looking for any files in the current directory ending with .java. On linux you can try something like this find -name "*.java" | xargs javac. The first part is a find command that searches recursively for all files ending with .java. It this pipe's that list of files to xargs which adds them all as individual arguments to the java command.

That really isn't the best way. Again, try maven. Its pretty simple to set up for a basic project, your IDE usually has a feature to create a maven project for you as well.

troymass
  • 1,022
  • 3
  • 11
  • 24
0

to compile more than one file having different package name in same folder use

javac -d . *.java
Artjom B.
  • 61,146
  • 24
  • 125
  • 222