Accessing classes within a JAR file?

U

utsavprabhu

Hi,

I have packaged my Java application using OneJAR, and I now need to
run some test scripts against this JAR file. However, I find my
classes virtually inaccessible within the jar. How do I get to them?

Regards,
superutsav
 
A

Andrew Thompson

I have packaged my Java application using OneJAR, and I now need to
run some test scripts against this JAR file. However, I find my
classes virtually inaccessible within the jar. How do I get to them?

I suggest you either ..
a) Repack the single Jar using the SDK's own jartool, or..
b) Take it up with 'the makers of OneJAR' (read 'someone
who cares that it fails').

Alternately, you might take the much wider utility
solution of keeping the jar's separate but ensuring
that the ancillary Jar's are on the classpath when
the main Jar is invoked (there are a number of ways
to avhieve that, including a Jar manifest, or using
web start to launch the application).

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
U

utsavprabhu

I suggest you either ..
a) Repack the single Jar using the SDK's own jartool, or..
b) Take it up with 'the makers of OneJAR' (read 'someone
who cares that it fails').

Alternately, you might take the much wider utility
solution of keeping the jar's separate but ensuring
that the ancillary Jar's are on the classpath when
the main Jar is invoked (there are a number of ways
to avhieve that, including a Jar manifest, or using
web start to launch the application).

Hey thanks a lot for the suggested solutions! I think it's time for me
to figure out how to write a jar manifest.

Regards,
superutsav
 
A

Andrew Thompson

...I think it's time for me
to figure out how to write a jar manifest.

I am more experienced with the web start* way
of doing things, for a good overview of manifests,
you might try..
<http://java.sun.com/docs/books/tutorial/deployment/jar/>
..particularly the link to section titled "Working
with Manifest Files: The Basics".

* Web start does not need the manifest, but will probably
introduce other hurdles - e.g. most web start apps. need
to be digitally signed, unless they are very trivial. You are
probably better off figuring how to use the manifest.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
R

Roedy Green

Hey thanks a lot for the suggested solutions! I think it's time for me
to figure out how to write a jar manifest.

have a look at ant, jar.exe and genjar. You can examine the those
jars with Winzip or other Zip archiver.

One-Jar has a custom ClassLoader and a custom Jar format. I can't see
the compensating benefit. Further the instructions on use seem even
more mysterious than ordinary jar building.


See http://mindprod.com/jgloss/ant.html
http://mindprod.com/jgloss/jarexe.html
http://mindprod.com/jgloss/genjar.html
http://mindprod.com/jgloss/onejar.html
 
B

brian.vanheesch

Hi,

I have packaged my Java application using OneJAR, and I now need to
run some test scripts against this JAR file. However, I find my
classes virtually inaccessible within the jar. How do I get to them?

Regards,
superutsav

Not sure about OneJar specifically, but I would question why you need
to deliver you application as a single jar file. If you are including
3rd party tools (including open soource), then you will need to review
the individual tool's license requirements. Some products require
that it is included in unmodified.

Barring that, I use 2 mechanisms to extract specific files from jar:

1) Access as a classpath resource:

InputStream is = Object.class.getResourceAsStream ( fileName ) ;

2) Access as a jar file entry (you can get the jarfile from
System.getProperty( "java.class.path" ) & StringTokenizer it):

JarFile jar = new JarFile ( jarFileName ) ;
JarEntry entry = jar.getJarEntry ( fileName ) ;
InputStream input = jar.getInputStream ( entry ) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top