3

When I use javac to compile many independent .java file, I find if one fails, no .class file will generate. For example, I try this command:

javac A.java B.java C.java -Xmaxerrs 200 -Xmaxwarns 200

There are no dependencies among these *.java files. When I use the command above to compile these *.java files, I find:
Case 1: All of the *.java files are correct. I will get A.class, B.class and C.class after javac's compilation.
Case 2: A.java has some errors, B.java and C.java are both correct. After compilation, I can't get any .class file.

How can I get B.class and C.class after javac's compilation in Case 2? Is there any javac option to solve this problem?

jimmyzhao
  • 41
  • 3

4 Answers4

2

Remove A.java and check the output if it work then check the error of A.java and recompile

After removing a.java compiler not work then try to debugging main java codes.

  • Thanks for reply!But it's not a proper solution, since I'm writing a program which need to compile a list of independent Java source code files "as much as possible" automatically.Do you have another suggestion? – jimmyzhao Aug 27 '17 at 09:07
1

Workaround: IDEs such as Eclipse or IntelliJ compile "as much as possible". They even allow you to run code that doesn't completely compile!

Alternatively, you could look into using a build system such as maven, gradle, ... - such tools will for sure allow for this.

( and for any project of reasonable size, using a build system is "mandatory" anyways - because of that: directly using javac manually is just a pain, and makes things that should be easy to do pretty complicated sometimes )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks for reply!But I can't use this workaround approach, since I'm writing a program which need to compile a list of independent Java source code files "as much as possible" automatically.I can't embed Eclipse or IntelliJ in my program, do you have another suggestion? – jimmyzhao Aug 27 '17 at 09:05
  • You don't use eclipse as part of your program. It is an ide supporting you in the process of creating Java applications! In that sense I don't get your problem. – GhostCat Aug 27 '17 at 09:17
0

If there are dependencies of A to B or C, and class A is having some compilation issue, then B.class and C.class also won't be generated. Fix the compilation issue in A or clear the dependencies.

nagendra547
  • 5,672
  • 3
  • 29
  • 43
  • As @user1133981 mentioned there are no dependencies. Java compiler abort compiling if any error occurs. Even if the error occurs in C.java no class files will be generated. – Obenland Aug 04 '17 at 06:55
  • Thanks for reply. There are no dependencies among A.java, B.java and C.java. How can I solve this problem? – jimmyzhao Aug 04 '17 at 06:56
0

As there are no dependencies among the source code files, the simplest solution I can think of is to invoke javac once for each source file.


As an alternative you can use ECJ, the compiler used by the Eclipse IDE and part of JDT Core. From the linked page:

In particular, it allows to run and debug code which still contains unresolved errors.

This is one of the main differences between javac and the Eclipse compiler.

It is available as a separate download (see section JDT Core Batch Compiler for any build linked from the download page). There are help pages describing how to use the compiler programmatically or as a separate application from the command line.

siegi
  • 5,646
  • 2
  • 30
  • 42