简体   繁体   中英

Programatically add class to existing JAR file

I get path to .jar file as user input of my java application. I need to add one class that is on my class path to that .jar file and then send the .jar file to external service (over network). The service on the other site must be able t load classes from the .jar file including the one I added in my application.

The solution should have these properties:

  • universal: should work for any thinkable .jar containing classes user can give me.
  • automatic: class addition must by done by my java application.

The class to be added to .jar file has name that does not collide with any other class in that JAR.

由于jar是常规zip文件,因此可以使用java.util.zip提供的功能。

要补充其他人的意见,如果您需要在另一端加载JAR,则应检查http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html

I understand that you have a class on your classpath, and what you want to do is to get its bytearray representation for the purpose of injecting it into an existing JAR file.

Neat.

Hava a look at the javax.tools package, available with JDK 1.6 and up:

http://docs.oracle.com/javase/6/docs/api/javax/tools/package-summary.html

You can programmatically compile code and serialize bytecode using mechanisms available there. If not, ASM could be of help as well: http://asm.ow2.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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