如何在jar文件中使用类文件? [英] how to use class files inside a jar file?

查看:125
本文介绍了如何在jar文件中使用类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 eclipse ,jar文件位于〜/ zmx / alg4 / algs4.jar中,我需要的类文件是algs4.jar / A.class。我的项目存在于〜/ workspace / test中。 jar文件已经在参考库中,如何在我的项目中使用类文件?我已经尝试过:

I'm using eclipse, the jar files locate in ~/zmx/alg4/algs4.jar, the class file I need is algs4.jar/A.class. my project exists in ~/workspace/test. the jar file are already in Referenced Libraries, how can I use the class file in my project? I've already tried:

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

但它不起作用。

推荐答案

您不能在命名包中使用默认包中的类:[import]在任何尝试导入未命名包中的类型时都需要编译时错误。您可能希望通过反射(见下文)访问它,或者如果您有源代码,则重新打包您的库algs4.jar。

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". You may want to access it via reflection (see below), or repackage your library algs4.jar if you have the source code.

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

这篇关于如何在jar文件中使用类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆