Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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?  
0
jjacksn
Asked:
jjacksn
  • 9
  • 4
  • 3
  • +1
3 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now