3

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?

2
  • 3
    For larger projects you should think of automating the build process. E.g. using a build tool like Maven, Gradle or Ivy. Commented Sep 17, 2012 at 19:48
  • 2
    I would use a build system like maven This will allows you do build complex and download libraries as required. Commented Sep 17, 2012 at 19:49

3 Answers 3

6

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

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

Comments

3

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.

Comments

1

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

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.