Nick
Nick

Reputation: 597

Compiling Java program with multiple classes on Linux

So far I have been able to compile my Java programs in Linux using javac command in Terminal, but soon I'll need to compile a program that has two or three classes in it along with main and I'm not sure how this will work.

Can I still use javac command for this?

Upvotes: 3

Views: 9154

Answers (3)

Stephen Connolly
Stephen Connolly

Reputation: 14116

Ideally you would use a build system such as Maven, ANT, etc

If you are just compiling classes which are in the current working directory, and you have not used packages, you can quite happily use

$ javac *.java

If you have used some packages (and have put the files in their correct package directories) you can use

$ javac $(find . -name \*.java)

When you get to a large number of files, you will need to list them in a file and reference that via a @ argument, eg

$ find . -name \*.java > ./java-files.txt
$ javac @./java-files.txt

But ultimately a build tool will make life a lot easier.

Upvotes: 3

Mark Meyer
Mark Meyer

Reputation: 3733

Yes you can. From the oracle javac page

There are two ways to pass source code file names to javac:

For a small number of source files, simply list the file names on the command line.

For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.

See the documentation

Upvotes: 1

Keith Randall
Keith Randall

Reputation: 23265

Yes, just do javac *.java (if all your classes are in the default package).

Upvotes: 6

Related Questions