简体   繁体   English

如何使用内部类编译Java类?

[英]How to compile java class with inner classes?

I need to compile a java class that has multiple inner classes (or nested). 我需要编译一个具有多个内部类(或嵌套的)的Java类。 Example: Myclass.java Myclass$sub1.class Myclass$sub2.class Myclass$sub2$subsub.class 示例: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. 我需要使用已编译的内部类文件来编译文件Myclass.java(命令行,oracle javac)。

How can this be done? 如何才能做到这一点? I tried the -sourcepath option, with no luck. 我尝试了-sourcepath选项,没有运气。

Thanks 谢谢

You compile a source file , not a source class . 您编译源文件 ,而不是源 Any classes, including nested ones, in that source file are compiled. 该源文件中的所有类(包括嵌套类)都将被编译。 No special effort required. 无需特别的努力。

javac MyClass.java 

只需编译源文件,它也会自动编译内部类。

The direct and striaght way would be to use javac Myclass.java 直接和直接的方法是使用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 这可能会有所帮助: https : //stackoverflow.com/a/70358/2908301

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM