简体   繁体   English

如何在jar文件中使用类文件?

[英]how to use class files inside a jar file?

I'm using eclipse , the jar files locate in ~/zmx/alg4/algs4.jar, the class file I need is algs4.jar/A.class. 我正在使用eclipse ,jar文件位于〜/ zmx / alg4 / algs4.jar中,我需要的类文件是algs4.jar / A.class。 my project exists in ~/workspace/test. 我的项目存在于〜/ workspace / test中。 the jar file are already in Referenced Libraries , how can I use the class file in my project? jar文件已经在Referenced Libraries中 ,如何在我的项目中使用类文件? I've already tried: 我已经尝试过:

import A; 
private A variable = new A();

but it doesn't work. 但这不起作用。

You can't use classes in the default package from a named package: "[import] require a compile-time error on any attempt to import a type in an unnamed package" . 您不能使用命名包中的默认包中的类: “ [import]在任何尝试导入未命名包中的类型时都要求编译时错误” You may want to access it via reflection (see below), or repackage your library algs4.jar if you have the source code. 您可能希望通过反射来访问它(见下文),或者如果您有源代码,则将您的库algs4.jar重新打包。

 Class.forName("SomeClass").getMethod("someMethod").invoke(null);

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

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