How to add a class to a Jar
Page 1 of 12 Replies - 86 Views - Last Post: 07 February 2012 - 07:02 PM
Topic Sponsor:
#1
How to add a class to a Jar
Posted 07 February 2012 - 06:21 PM
(I would consider my knowledge of Java strong, so you don't have to baby me
) Basically, I want to know how to add a .class file into a certain package in a .jar file. Ultimatly what I would ideally want is for the user to be able to code a specialized script extending from my jar and then be able to run the jar from the desktop and their code be executed. I have a feeling a little bit of ASM may be needed for compiling but I just thought i would ask for advice here. Thanks.
Replies To: How to add a class to a Jar
#2
Re: How to add a class to a Jar
Posted 07 February 2012 - 06:36 PM
This is where you would want/need to use the Reflection API. This lets you create classes from URLS, check the methods of the class, and invoke methods. If you have their class implement your interface, then you know for sure it has the methods you want it to.
#3
Re: How to add a class to a Jar
Posted 07 February 2012 - 07:02 PM
What do you mean script? Do you mean Java code? If so, then you use the JavaCompiler API to compile source code. Here's a facade you can use to compile source code. It's the JavaPlatform class.
You could write the class file and manifest into the JAR using a ZipOutputStream, since a JAR is a zip archive. Another easier, but less portable way of getting the files into the jar is by using a ProcessBuilder and calling the jar command. But of course, that's not a working solution if you distribute your application.
No assembly needed.
You could write the class file and manifest into the JAR using a ZipOutputStream, since a JAR is a zip archive. Another easier, but less portable way of getting the files into the jar is by using a ProcessBuilder and calling the jar command. But of course, that's not a working solution if you distribute your application.
No assembly needed.
This post has been edited by blackcompe: 07 February 2012 - 07:02 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|