How to compile multiple java source files in command line
Asked Answered
C

5

67

I know running javac file1.java produces file1.class if file1.java is the only source file, then I can just say java file1 to run it.

However, if I have 2 source files, file1.java and file2.java, then how do I build the program?

Chubb answered 26/1, 2011 at 1:58 Comment(2)
Does that mean both the files have main method or there are 2 entrypoint of an application?Adverse
#6623661Siegler
R
142

Try the following:

javac file1.java file2.java
Relationship answered 26/1, 2011 at 2:0 Comment(1)
this is hardcoded, how do we use the results from $(find .) etc?Gametocyte
R
101

or you can use the following to compile the all java source files in current directory..

javac *.java
Ransom answered 26/1, 2011 at 7:59 Comment(3)
No, it is not recursive.Continence
* is the symbol for a wildcard. It means all things in this directory that end with ".java"Influence
this is not recursive but it is not badVaal
W
7

Here is another example, for compiling a java file in a nested directory.

I was trying to build this from the command line. This is an example from 'gradle', which has dependency 'commons-collection.jar'. For more info, please see 'gradle: java quickstart' example. -- of course, you would use the 'gradle' tools to build it. But i thought to extend this example, for a nested java project, with a dependent jar.

Note: You need the 'gradle binary or source' distribution for this, example code is in: 'samples/java/quickstart'

% mkdir -p temp/classes
% curl --get \
    http://central.maven.org/maven2/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar \
        --output commons-collections-3.2.2.jar

% javac -g -classpath commons-collections-3.2.2.jar \
     -sourcepath src/main/java -d temp/classes \
      src/main/java/org/gradle/Person.java 

% jar cf my_example.jar -C temp/classes org/gradle/Person.class
% jar tvf my_example.jar
   0 Wed Jun 07 14:11:56 CEST 2017 META-INF/
  69 Wed Jun 07 14:11:56 CEST 2017 META-INF/MANIFEST.MF
 519 Wed Jun 07 13:58:06 CEST 2017 org/gradle/Person.class
Watkins answered 7/6, 2017 at 12:8 Comment(0)
S
2

1.use wildcard

2.use options

3.https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html

Shook answered 1/5, 2018 at 2:53 Comment(2)
How could this work? @file expects the file provided to contain the names of the files to compile.Phosphatize
sorry, i forget to check my answer. now, i paste official description.Shook
C
-7

OR you could just use javac file1.java and then also use javac file2.java afterwards.

Confirmation answered 4/7, 2017 at 13:21 Comment(2)
That does not answer the question. OP was asking if there is a way to compile multiple .java files at once.Sybille
what if there are 1000 files? javac *.java is best. Unfortunately it is not recursiveNuzzle

© 2022 - 2024 — McMap. All rights reserved.