是否可以从main内部获取包含我的主要功能的.class文件的路径?
URL main = Main.class.getResource("Main.class");
if (!"file".equalsIgnoreCase(main.getProtocol()))
throw new IllegalStateException("Main class is not stored in a file.");
File path = new File(main.getPath());
请注意,大多数类文件都被组装成JAR文件,因此,并非在所有情况下都有效(因此IllegalStateException
)。但是,您可以找到包含这一技术类的JAR,你可以通过调用代获得类文件的内容getResourceAsStream()
代替getResource()
,而将工作的类别是在文件系统或JAR 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句