Embedding a resource in a JAR

so here is a what I want to do:

1.  Create an jar file from my java classes.
2.  Create an external application to bundle the installer (ant or nsis)
3.  During the creation of the installer, I want to insert a file with a randomly generated key into the JAR file as a resource.
4.  During execution of the jar file, I want to be able to retrieve this key file, and know that it might not be there sometimes.

what is the best way to do this?  
LVL 5
jjacksnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
So where is the problem really? Ant can do all this
0
WebstormCommented:
4.

InputStream ins=<yourclass>.class.getResourceAsStream("resourcefilename");
if (ins==null) // not found
else // ... read from inputstream ins
0
WebstormCommented:
I think 3. should be done before 2. :

1.  Create an jar file from my java classes.
2.  insert a file with a randomly generated key into the JAR file as a resource.
3.  Create an external application to bundle the installer (ant or nsis)
4.  During execution of the jar file, I want to be able to retrieve this key file, and know that it might not be there sometimes.

then 1. and 2. can be done in one command line using "jar" tool
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

jjacksnAuthor Commented:
ok, sorry, very inexperienced in using jars and resoureces.  

1.  do you happen to have a link on hand about what the command line would be
2.  Will InputStream ins=<yourclass>.class.getResourceAsStream("resourcefilename") search all loaded jars, the jar that contains the <yourclass>, or something else?
3.  How can debug this through eclipse (as it is running the class files)
4.  i'd actually like to combine 2 and 3, but i can easily do that in a script if I can add the key file to the jar via the command line.
0
WebstormCommented:
>> do you happen to have a link on hand about what the command line would be
1.
to get command syntax:
  jar -help

2. the jar that contains the <yourclass> (same package too)

0
WebstormCommented:
>> if I can add the key file to the jar via the command line.
jar uf  <yourjar.jar> <keyfile>
0
WebstormCommented:
3. you can put a breakpoint just before   if (ins==null)   and view value of 'ins' variable
0
jjacksnAuthor Commented:
WebStorm, i think i did a poor job phrasing 3.  in eclipse, it won't be debugging through a jar file, but using the compiled class files.. so the call will always fail when i'm testing it unti i create the jar, at which point i won't be able to use the debugger... or is that wrong?
0
objectsCommented:
> or is that wrong?

It is, as long as the resorce is available to the build path in eclipse it wil work.
ie. have the resource in a source folder
and then when you build a jar include that source folder in the jar

0
WebstormCommented:
>>  so the call will always fail when i'm testing it unti i create the jar
No, if you use, for example :

     ins=MyClass.class.getResourceAsStream("keyfile.dat");

then (in jar, or not in jar) the file "keyfile.dat" must be in the same directory as file "MyClass.class"

     ins=MyClass.class.getResourceAsStream("dir/keyfile.dat");

then (in jar, or not in jar) the file "keyfile.dat" must be in a directory named "dir" which must be in the same directory as file "MyClass.class"
0
jjacksnAuthor Commented:
very dumb question... once i have the input stream how do i read it all into a byte array?  i can't determine the size of the stream if it is just an InputStream first, so i don't know how large to make the byte array.
0
CEHJCommented:
Just read it all into a ByteArrayOutputStream - it will resize automatically to fit
0
WebstormCommented:
0
WebstormCommented:
You need to read from the inputstream and write it to the ByteArrayOutputStream :

    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    byte[] buff=new byte[8192]; // buffer to read resource
    int sz;
    while ((sz=ins.read(buff))>0)
           baos.write(buff,0,sz);
    ins.close();
    byte[] data=baos.toByteArray();
0
CEHJCommented:
>>while ((sz=ins.read(buff))>0)

should be

while ((sz=ins.read(buff))>=0)
0
CEHJCommented:
:-)
0
WebstormCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.