How to compile multiple Java files together?
Replace
java *.java
with
javac *.java
javac
is the command to compile a .java
file and since you want to apply it to all files, you specify *.java
.
javac *.java
compiles all your java files in that folder. In order to run the one with public static void main
, you need to use java Foo
where Foo.java
is the file that has public static void main
.
Demo:
[~/Desktop/quickstart]: ls
A.java B.java C.java
[~/Desktop/quickstart]: javac *.java
[~/Desktop/quickstart]: ls
A.class A.java B.class B.java C.class C.java
[~/Desktop/quickstart]: java C
Hello
Hi
A.java:
class A {
public void hello() {
System.out.println("Hello");
}
}
B.java:
class B {
public void hi() {
System.out.println("Hi");
}
}
C.java:
class C {
public static void main(String[] args) {
A a = new A();
a.hello();
B b = new B();
b.hi();
}
}
Compiling multiple packages using the command line in Java
This should do it (may require additional classpath elements via the -cp
command line switch):
javac Support/*.java Me/*.java Wrapers/*.java
But if your build process gets more complex (and it will!), you should look into using Apache Ant for build automation.
javac option to compile all java files under a given directory recursively
I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).
Using Javac
If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac
offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac
with the @
prefix.
If you can create a list of all the *.java
files in your project, it's easy:
# Linux / MacOS
$ find -name "*.java" > sources.txt
$ javac @sources.txt
:: Windows
> dir /s /B *.java > sources.txt
> javac @sources.txt
- The advantage is that is is a quick and easy solution.
- The drawback is that you have to regenerate the
sources.txt
file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.
Using a build tool
On the long run it is better to use a tool that was designed to build software.
Using Ant
If you create a simple build.xml
file that describes how to build the software:
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin"/>
</target>
</project>
you can compile the whole software by running the following command:
$ ant
- The advantage is that you are using a standard build tool that is easy to extend.
- The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.
Using Maven
Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.
- It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.
- The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).
Using an IDE
Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.
They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException
.
One additional note
For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse
command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)
Compiling Multiple Classes (Console) in Java
Just do
javac *.java
Or if you have separate source and binary folders:
mkdir bin
javac -d bin src/*.java
Or if you have multiple source folders:
mkdir bin
shopt -s globstar # requires bash 4
javac -d bin src/**/*.java
Compiling multiple jar and java files using javac
You need to stop the shell from globbing
the wild-card in lib/*.jar
by escaping it.
Also, you need to remove the .jar
suffix ... because that's how classpath wildcards work; see Oracle's "Setting the classpath" document.
So ...
javac -classpath lib/\* src/*.java
Using an IDE is another option. However, if all you want to do is compile and run, then downloading and installing and learning to use an IDE is overkill (IMO). And the flipside is that it is good for an IDE-using Java programmer to also understand how to compile and run from the shell prompt ...
How to compile and run Java code with multiple packages or source files
Try the following
javac -classpath ./lib/*.jar ./src/*/*.java
Give a proper jar library path, if your java classes use a 3d party libraries. It's better to use a build tool with proper directory structure.
Compiling four java files within one package using javac
From the project's root directory:
javac src/com/osama/GHide/*.java
To run, assuming no other dependencies:
java -cp ./src com.osama.GHide.EnteringPoint
(Assuming EnteringPoint
has the normal main
function.)
The javac
command compiles all the .java
files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class
files in the same directory, which may or may not be what you want.
To put them in a different directory, use the -d
option and supply a path.
javac -d bin src/com/osama/GHide/*.java
Then to run:
java -cp ./bin com.osama.GHide.EnteringPoint
Related Topics
JPA - Returning an Auto Generated Id After Persist()
What Is the Idiomatic Way to Compose a Url or Uri in Java
In Java 8, Why Is the Default Capacity of Arraylist Now Zero
Change Database Schema Used by Spring Boot
How to Copy the Contents of One Arraylist into Another
Parsing a Fixed-Width Formatted File in Java
What Is the Shortest Way to Pretty Print a Org.W3C.Dom.Document to Stdout
How to Implement a Fsm - Finite State MAChine in Java
How to Remove Element from Arraylist by Checking Its Value
When to Use a Constructor and When to Use Getinstance() Method (Static Factory Methods)
Does Java Have Any Mechanism for a Vm to Trace Method Calls on Itself, Without Using Javaagent, etc
"Faceted Project Problem (Java Version Mismatch)" Error Message
Accessing Constructor of an Anonymous Class
Refreshing Static Content with Spring MVC and Boot
Htmlunit Doesn't Wait for JavaScript