java 怎么import一个jar包里面的javaclass变成jar包

1.在有源码的情况下:就那hibernate-core-4.1.10.Final.jar这个jar来说吧,我想修改里面的org\hibernate\cfg\PropertyContainer.class文件
& & & &1)&在Myeclipse中建立一个web工程(web_test_5)
& & & &2)将该class文件(PropertyContainer.class) &对应的 java文件(PropertyContainer.java) &放入该web工程的src目录下(建好对应的包org.hibernate.cfg,把源文件放入该目录) &
& & & & & &
& & & &说明PropertyContainer.class源文件所在的目录:hibernate-release-4.1.10.Final\project\hibernate-core\src\main\java
& & & &3)将PropertyContainer.class所在的jar包Build Path到该工程中(因为该java文件可能会与其他的class文件相关联,所以要引入对应的jar包)
& & & &4)打开该java文件,接下来你想怎么改就怎么改,改好了保存(保存就行了,这样Myelipse就是自动帮你编译该java文件,并生成字节码放到指定的目录,
&嘿嘿,原来保存好了java文件,Myeclipse就会自动帮我们编译,我今天才发现)
& & & &5)到找到对应的web工程web_test_5所在的目录,到web_test\WebRoot\WEB-INF\classes\目录下找该java文件对应的class文件
& & & &6)解压源码所在的jar包,将该其中PropertyContainer.class替换掉就行
& & & &7)然后再打包成jar文件就行了
2.在没有源码的情况下,我们可以使用Jclassbin软件,Jclassbin可以用于改变class出的输出,但是对于改变类好像不行,因为我们不能往Jclassbin中import,所以我们如果用Jclassbin往一个jar包中java中的某个添加一个类后,在用这个jar包时就会出问题,
可能有办法import吧,可能是目前我还不知道吧,如果哪位大虾知道 请留言哦!(有机会再研究一下吧)
&1)说明:开始我是用的DJ Java Decompiler反编译 把我需要修改的那个PropertyContainer.class文件反编译出来把代码保存到Propertycontainer.java
&查看反编译得到的java文件和Hibernate提供的jar文件中java文件有点不同,比如Hibernate中java文件使用了泛型,你用这个软件反编译得到的文件就没使用泛型
,不知道什么情况,反正最后放弃用这个软件
&2)说明:然后直接在dos里面用javac PropertyContainer.java编译
&问题来了..
&因为引用了很多其他jar下面的方法 而且PropertyContainer.java有package
&3)使用Jclassbin
&打开Jclassbin,界面效果如下
阅读(...) 评论()本帖子已过去太久远了,不再提供回复功能。博客分类:
今天boss提了这样一个问题:在导入的两个jar包中有两个同包同类名的类,在调用时如何区分调用的是哪个?
以前学习时我们总是被告诫尽量避免同名类出现,没怎么了解这方面的问题。boss说只要将classpath中要调用的jar路径放在前面导入即可,不太理解。在网上查询后得知,这与JVM的加载有关:
包名类名都相同,那jvm没法区分了,一般ide是会提示发生冲突而报错的,如果不报错的,那就只有第一个包被引入(在classpath路径下排在前面的包),第二个包会在classloader加载类时判断重复而忽略。
在网上查询后得知在哪儿查到的?谢谢
hubusmile520
浏览: 10701 次
来自: 武汉
还是没有区分调用啊,如果我两个都要使用怎么办
&div class=&quote_title ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'> 博客详情
import&java.io.F
&*&删除项目中
&*&.class文件
&*&@author&Sheamus
public&class&DeleteClass&{
&&&&static&int&count&=&0;
&&&&public&static&void&main(String[]&args)&{&&&
&&&&&&&&//C:\\Users\\Sheamus\\Desktop\\itjds_org&(项目路径信息)
&&&&&&&&refreshFileList("C:\\Users\\Sheamus\\Desktop\\itjds_org");&&&&
&&&&&&&&System.out.println("共删除了:"&+&count&+&"个文件!");
&&&&public&static&void&refreshFileList(String&strPath)&{&&&&&&&&
&&&&&&&&File&dir&=&new&File(strPath);&&&&&&&&
&&&&&&&&File[]&files&=&dir.listFiles();&&&&&&&&&&&&&&&
&&&&&&&&if&(files&==&null)
&&&&&&&&&&&&System.out.println("该目录下没有任何一个文件!");
&&&&&&&&&&&&
&&&&&&&&}&&&&
&&&&&&&&for&(int&i&=&0;&i&&&files.&i++)&{&&&&&&&&&&&&
&&&&&&&&&&&&if&(files[i].isDirectory())&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&refreshFileList(files[i].getAbsolutePath());&&&&&&&&&&&&
&&&&&&&&&&&&}else&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&String&strFileName&=&files[i].getAbsolutePath().toLowerCase();
&&&&&&&&&&&&&&&&if(strFileName.endsWith(".class")){
&&&&&&&&&&&&&&&&&&&&System.out.println("正在删除:"&+&strFileName);&
&&&&&&&&&&&&&&&&&&&&files[i].delete();
&&&&&&&&&&&&&&&&&&&&count++;
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}&&&&&&&&
&&&&&&&&}&&&
import java.io.BufferedR
import java.io.BufferedW
import java.io.F
import java.io.FileInputS
import java.io.FileOutputS
import java.io.IOE
import java.io.InputStreamR
import java.io.OutputStreamW
&* 删除项目中
&* @author Sheamus
public class Delete {
&& &//static String tartgetDir = "D:\\";
&& &static String tartgetDir = "D:\\Test1\\itjds_server\\";
&& &//static String sourceDir = "C:\\Users\\Sheamus\\Desktop\\";
&& &static String sourceDir = "D:\\Test\\itjds_server\\";
&& &//static String projectName = "itjdsTest";
&& &static String projectName = "src";
&& &static String encoding = "utf-8";
&& &public static void main(String[] args) {
&& &&& &//C:\\Users\\Sheamus\\Desktop\\itjds_org (项目路径信息)
&& &&& &//源
&& &&& &String sourcePath = sourceDir + projectN
&& &&& &refreshFileList(sourcePath);
&& &public static void refreshFileList(String sourcePath) {
&& &&& &File dir = new File(sourcePath);
&& &&& &File[] files = dir.listFiles();
&& &&& &if (files == null)
&& &&& &&& &System.out.println("该目录下没有任何一个文件!");
&& &&& &&& &
&& &&& &for (int i = 0; i & files. i++) {
&& &&& &&& &if (files[i].isDirectory()) {
&& &&& &&& &&& &
&& &&& &&& &&& &//拼接路径
&& &&& &&& &&& &String absolutePath = files[i].getAbsolutePath();
&& &&& &&& &&& &String replace = absolutePath.replace(sourceDir, tartgetDir);
&& &&& &&& &&& &
&& &&& &&& &&& &//System.out.println(replace);
&& &&& &&& &&& &
&& &&& &&& &&& &File repFile = new File(replace);
&& &&& &&& &&& &//创建文件夹
&& &&& &&& &&& &if(!repFile.exists()) {
&& &&& &&& &&& &&& &repFile.mkdirs();
&& &&& &&& &&& &}
&& &&& &&& &&& &refreshFileList(files[i].getAbsolutePath());
&& &&& &&& &}else {
&& &&& &&& &&& &String strFileName = files[i].getAbsolutePath();
//&& &&& &&& &&& &if(strFileName.endsWith(".java")){
&& &&& &&& &&& &&& &//拼接路径
&& &&& &&& &&& &&& &String replace = strFileName.replace(sourceDir, tartgetDir);
&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &//System.out.println(replace);
&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &File repFile = new File(replace);
&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &if(!repFile.exists()) & &
&& &&& &&& &&& &&& &{ & &
&& &&& &&& &&& &&& & & &try { & &
&& &&& &&& &&& &&& & & &&& &repFile.createNewFile(); & &
&& &&& &&& &&& &&& & & &} catch (IOException e) { & &
&& &&& &&& &&& &&& & & & & &// TODO Auto-generated catch block & &
&& &&& &&& &&& &&& & & & & &e.printStackTrace(); & &
&& &&& &&& &&& &&& & & &} & &
&& &&& &&& &&& &&& &} & &
&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &InputStreamReader read =
&& &&& &&& &&& &&& &BufferedReader bufferedReader =
&& &&& &&& &&& &&& &OutputStreamWriter out =
&& &&& &&& &&& &&& &BufferedWriter writer =
&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &read = new InputStreamReader(new FileInputStream(files[i]),encoding);
&& &&& &&& &&& &&& &&& &bufferedReader = new BufferedReader(read);
&& &&& &&& &&& &&& &&& &String lineTxt =
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &out = new OutputStreamWriter(new FileOutputStream(repFile),encoding);
&& &&& &&& &&& &&& &&& &writer = new BufferedWriter(out);
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &while((lineTxt = bufferedReader.readLine()) != null){
&& &&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &&& &if(lineTxt.startsWith("/*") && lineTxt.contains("*/") ) {
&& &&& &&& &&& &&& &&& &&& &&& &String substring = lineTxt.substring(lineTxt.indexOf("*/") + 2);
&& &&& &&& &&& &&& &&& &&& &&& &writer.append(substring+"\n");
&& &&& &&& &&& &&& &&& &&& &&& &writer.flush();
&& &&& &&& &&& &&& &&& &&& &} else {
&& &&& &&& &&& &&& &&& &&& &&& &writer.append(lineTxt + "\n");
&& &&& &&& &&& &&& &&& &&& &&& &writer.flush();
&& &&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &&& &/*if(lineTxt.startsWith("/* Location:") || lineTxt.startsWith(" * Qualified Name:") || lineTxt.startsWith(" * JD-Core Version:")||) {
&& &&& &&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &&& &}*/
&& &&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &} catch (Exception e) {
&& &&& &&& &&& &&& &&& &System.out.println("读取文件内容出错");
&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &}finally{
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &if(writer != null) {
&& &&& &&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &&& &&& &writer.close();
&& &&& &&& &&& &&& &&& &&& &} catch (IOException e) {
&& &&& &&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &if(out != null) {
&& &&& &&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &&& &&& &out.close();
&& &&& &&& &&& &&& &&& &&& &} catch (IOException e) {
&& &&& &&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &if(bufferedReader != null) {
&& &&& &&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &&& &&& &bufferedReader.close();
&& &&& &&& &&& &&& &&& &&& &} catch (IOException e) {
&& &&& &&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &&& &if(read != null) {
&& &&& &&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &&& &&& &read.close();
&& &&& &&& &&& &&& &&& &&& &} catch (IOException e) {
&& &&& &&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &}
&& &&& &&& &&& &&& &&& &
&& &&& &&& &&& &&& &}
//&& &&& &&& &&& &}
&& &&& &&& &}
”在线下联结了各位 OSCer,推广开源项目和理念,很荣幸有你的参与~
领取条件:参与过开源中国“源创会”的 OSCer 可以领取
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥如何获取一个jar包里面的所有的类信息? - ITeye问答
我知道要获取一个类的方法与属性信息可以通过反射的方式获得,但要获取一个jar包里面的类信息,简单的说就是要得到一个jar包里面的所有的类的名称。不知道该怎么获得呢
public static Class[] getClassFromPackage(String entirePackagePath);
Class thisClazz = ClassUtils.
String thisName = thisClazz.getName();;
String thisFileName = thisName.substring(thisName.lastIndexOf('.'); +
1); + ".class";
URL url = thisClazz.getResource(thisFileName);;
String strUrl = url.toString();;
boolean isInJar =
//get classes dictionary or get jar file who contains the classes
if(strUrl.substring(0 , 4);.equals("jar:");); {
strUrl = strUrl.substring(10);;
int index = strUrl.indexOf(".jar");;
strUrl = strUrl.substring(0 , index + 4);;
//use in the develop process,
//in the run time the class must in the jar
strUrl = strUrl.substring(6);;
int index = strUrl.lastIndexOf("classes");;
strUrl = strUrl.substring(0 , index + 7);;
List classes = new ArrayList();;
sun.tools.java.ClassPath classPath = new ClassPath(strUrl);;
Identifier iden = Identifier.lookup(entirePackagePath);;
Class stateC
sun.tools.java.Package pkg = new sun.tools.java.Package(classPath ,
java.util.Enumeration e = pkg.getBinaryFiles();;
String classFileN
int classI
String classN
for(; e.hasMoreElements();; ); {
classFileName = ((ClassFile);e.nextElement(););.getName();;
if(isInJar); {
String tmp = classFileName.substring(entirePackagePath.
length(); + 1);;
//在jar中运行是。会递归取得包下的Class
//开发环境时则不会
if(tmp.indexOf('/'); != -1); {
classIndex = classFileName.lastIndexOf('.');;
classFileName = classFileName.substring(0 , classIndex);;
//在jar中运行时,取得的classFileName为XXX/XXX/...../ActivityState
//在开发环境时 是 ActivityState
if(isInJar); {
className = classFileName.replace('/' , '.');;
className = entirePackagePath + "." + classFileN
stateClass = Class.forName(className);;
classes.add(stateClass);;
catch(IOException ex); {
System.out.println("未找到包路径");;
ex.printStackTrace();;
catch(ClassNotFoundException ex); {
System.out.println("未找到需要加载的类");;
ex.printStackTrace();;
Class[] result = new Class[classes.size();];
...........
来个简单点的!参考下
JarFile jfile = new JarFile("E:/some.jar");;
Enumeration files = jfile.entries();;
while(files.hasMoreElements(););
JarEntry entry = (JarEntry);files.nextElement();;
if(entry.getName();.startsWith("some/some/some"););
System.out.println(entry.getName(););;
如果你看过一些框架的源码,就知道大多数情形时他们都是先找到某个包的对应的文件位置,并遍历其下面的文件,读取其文件名,对应的就是类名。
@(#)PackageUtil.java
Technology
java.io.FileInputS
java.io.IOE
java.util.ArrayL
java.util.L
java.util.jar.JarE
java.util.jar.JarInputS
PackageUtil
IOException
main(String[]
IOException
List&String&
getClassInPackage("java.util");
for(String
System.out.println(s);
List&String&
getClassInPackage(String
List&String&
ArrayList&String&();
pkgName.replace('.',
CLASS_PATH_ARRAY)
if(!classPath.exists())
(classPath.isDirectory())
File(classPath,
if(!dir.exists())
dir.listFiles())
(file.isFile())
file.getName();
pkgName+"."
+clsName.substring(0,
clsName.length()
ret.add(clsName);
FileInputStream
FileInputStream(classPath);
JarInputStream
JarInputStream(fis,
jis.getNextJarEntry())
e.getName();
(eName.startsWith(rPath)
!eName.endsWith("/"))
ret.add(eName.replace('/',
'.').substring(0,eName.length()-6));
jis.closeEntry();
jis.close();
(Exception
RuntimeException(e);
CLASS_PATH_PROP
"java.class.path",
"java.ext.dirs",
"sun.boot.class.path"
List&File&
CLASS_PATH_ARRAY
getClassPath();
List&File&
getClassPath()
List&File&
ArrayList&File&();
(System.getProperty("os.name").indexOf("Windows")
CLASS_PATH_PROP)
System.getProperty(pro).split(delim);
ret.add(new
File(path));
已解决问题
未解决问题

我要回帖

更多关于 java class打包jar 的文章

 

随机推荐