-1

I need to compile a java class that has multiple inner classes (or nested). Example: Myclass.java Myclass$sub1.class Myclass$sub2.class Myclass$sub2$subsub.class

I need to compile (command line, oracle javac) the file Myclass.java, using the compiled inner class files.

How can this be done? I tried the -sourcepath option, with no luck.

Thanks

xgon
  • 175
  • 1
  • 3
  • 11
  • 4
    `javac MyClass.java` -- (Is this a trick question?) – Hot Licks Oct 23 '13 at 03:06
  • no it's not. using javac MyClass.java i get errors on the references of the nested classes! – xgon Oct 23 '13 at 03:16
  • 1
    What errors are you getting? Post them. – PM 77-1 Oct 23 '13 at 03:18
  • error: Cannot find symbol. Example: public static boolean initialize(SUBCLASS.SUBSUBCLASS t) ^ symbol: class SUBSUB location: class SUBCLASS r\cbri\kern\int\MyCLass.java:114: error: cannot find symbol – xgon Oct 23 '13 at 03:26
  • 1
    This sounds like a problem with how you're trying to refer to classes in your code rather than the compiler failing to see nested classes. An [SSCCE](http://sscce.org) would let someone point out your exact problem. – Ryan Stewart Oct 23 '13 at 04:04
  • That's what's known as a compile error. – Hot Licks Oct 23 '13 at 09:21

3 Answers3

9

You compile a source file, not a source class. Any classes, including nested ones, in that source file are compiled. No special effort required.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • i want to compile Myclass.java (source), BUT the nested classes are already compiled (Myclass$sub1.class Myclass$sub2.class Myclass$sub2$subsub.class) – xgon Oct 23 '13 at 03:09
  • They'll be overwritten if they're in the output directory of the compiler. If you want to use these existing ones instead of the compiled ones for whatever reason, then either replace the compiled class files with the existing ones after compiling or else run `java` with the existing ones on your classpath before the compiled ones. The first matching class wins on the classpath. – Ryan Stewart Oct 23 '13 at 04:01
0
javac MyClass.java 

Just compile the source file it will automatically compile the inner classes as well.

Trying
  • 14,004
  • 9
  • 70
  • 110
0

The direct and striaght way would be to use javac Myclass.java

Please make sure to check the modifiers being used incase you are experiencing errors while referencing the inner classes. This could be helpful : https://stackoverflow.com/a/70358/2908301

Community
  • 1
  • 1
eros
  • 103
  • 1
  • 11