Results 1 to 14 of 14
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Couple more questions on JAR Files :S

    Many apologies if it seems like I'm spamming the forums. I just seem to stumble upon many questions and there are always some questions that google cannot answer (or I suck at googl'ing). Anyhow, these are my questions:

    1. Signing JAR files ( Signing JAR Files (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files) ) What type of extension does the .keystore file need? What format is used to store the storepass and keypass in the .keystore file? Does it need headers like the manifest file? An example would suffice ;)

    2. So I made my package into a JAR file. The container (JFrame) has an image instead of the JAVA coffee symbol by the title (used BufferedImage). Think it would be better explained by displaying the code:

    Java Code:
     
    package sudoku;
    public class SudokuFrame extends JFrame {
         ....
         
         public SudokuFrame() {
                this.setIcon(getIcon());
                ....
         }
    
          	public BufferedImage getIcon() {
    		BufferedImage sudokuIcon = null;
    		try {
    		    sudokuIcon = ImageIO.read(new File("sudoku\\aziz_icon.png"));
    		} 
    		catch (IOException e) {}
    		return sudokuIcon;
    	}
           .....
    When I do java -jar SudokuJar.jar in cmd, the custom icon is displayed! But, when I make a project in Eclipse, go to properties (for that project) > Java Build Path > Add External Jars > SudokuJar.jar, make a driver program with the following content:

    Java Code:
    import sudoku.SudokuWorld;
    
    public class test {
    	public static void main (String[] args) {
    		SudokuWorld test = new SudokuWorld();
    		test.display();
    	}
    }
    THE ICON IS GONE! (Application is still displayed btw) I'm thinking the location of the image changed. If so, where to? And how can I make the icon display in the eclipse driver program? If not, I would still like to know what's wrong lol.

    Thanks in advance.
    Last edited by Lil_Aziz1; 05-31-2010 at 05:05 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    You cannot use java.io.File lib with unsigned applet ;)

    the code shouldn't actually work in your browser sandbox ;)

    I mean this code block

    Java Code:
    public BufferedImage getIcon() {
    		BufferedImage sudokuIcon = null;
    		try {
    		    sudokuIcon = ImageIO.read(new File("sudoku\\aziz_icon.png"));
    		} 
    		catch (IOException e) {}
    		return sudokuIcon;
    	}
    P.S> I dearly hope I understood the question...
    Last edited by Webuser; 05-31-2010 at 06:57 AM.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Sorry about any ambiguity in question #2. Let me try again:

    My java application has one package, called sudoku. I converted my package into a JAR file (it is unsigned at the moment) called SudokuJar.jar. The JAR file also has a Main-Class called DriverProgram. The manifest file DOES contain this header:
    Java Code:
    Main-Class: sudoku.DriverProgram
    Now when I run the jar file in CMD with the following command:
    Java Code:
    java -jar SudokuJar.jar
    The getIcon() method returns aziz_icon.png. I can tell because the JAVA symbol on the top left corner was replaced by aziz_icon.png.

    Next, I made a new java project called TestProject in Eclipse. I added the JAR file SudokuJar.jar by doing the following:
    Java Code:
     Right click on TestProject > Properties > Java Build Path (on the left side) > Add External JARs.. 
          (on the ride side) > Locate SudokuJar.jar > Open > OK
    Then, I made a driver program in TestProject called test. The content of test is in the first post. When I run test, the java symbol on the top left corner is still there; consequently aziz_icon.png is not there.

    What's wrong?

    Thanks in advance.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    to add any images you have to use an anchor class. As a rule it is a.class and place it into images project package
    So then you can simply ref your app with this image as

    Java Code:
    import images;
    ...
    Image image=ImageIO.read(a.class.getResourceAsStream("image.PNG"));
    I recommend to read
    this How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    this ImageIO (Java Platform SE 6)
    and this Reading/Loading an Image (The Java™ Tutorials > 2D Graphics > Working with Images)

    P.S. Study to use Google, pal ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    to add any images you have to use an anchor class. As a rule it is a.class and place it into images project package
    That is just a silly hack. You can load anything in a .jar you want given either a relative path; relative to some class or an absolute path. Read the API documentation for, e.g. the getResourceAsStream( ... ) method.

    kind regards,

    Jos

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    in Eclipse
    SO the program works ok outside of Eclipse when you execute it from a command prompt but fails when in Eclipse? Recommendation: Don't use Eclipse to execute it.

    Can you add some debug code to the app to see where it is looking for the image. I'm sure Eclipse uses directories differently than you expect.

    BTW Java applications don't need permission to access things on a system. Signing is for browser loaded applets that want to access things.

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Exclamation

    Quote Originally Posted by JosAH View Post
    That is just a silly hack. You can load anything in a .jar you want given either a relative path; relative to some class or an
    It is not a hack at all :rolleyes: It is a simplest way to ref a resource within a jar file...

    As I was saying, that is how
    Java Code:
    InputStream in=a.class.getResourceAsStream("image.PNG"));
    the boy can get an inner jar resource file stream :) It's simply :D
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    It is not a hack at all :rolleyes: It is a simplest way to ref a resource within a jar file...
    The simplest way?

    Quote Originally Posted by Webuser View Post
    As I was saying, that is how
    Java Code:
    InputStream in=a.class.getResourceAsStream("image.PNG"));
    the boy can get an inner jar resource file stream :) It's simply :D
    The boy?

    Jos

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Why do you even need the a.class? I've always used this.getClass().getResourceAsStream(path) where path is the path to the file in the jar file.

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Cool

    Quote Originally Posted by StormyWaters View Post
    Why do you even need the a.class? I've always used this.getClass().getResourceAsStream(path) where path is the path to the file in the jar file.
    Oh my...
    Not to use this
    getResourceAsStream(path)
    but simply to import needed package ;)
    But you can do as you wish :rolleyes:
    Last edited by Webuser; 06-04-2010 at 10:03 PM.

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post

    The boy?

    Jos
    The girl? :rolleyes:
    Last edited by Webuser; 06-04-2010 at 09:30 PM.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    Oh my...
    Not to use this but simply to import needed package ;)
    But you can do as you wish :rolleyes:
    Show us the code you are using (without the getResourceAsStream( ... ) call mayhap? just importing a package?)

    Jos

  13. #13
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I guess I could see how it could be useful if the image folder is moved somewhere as you wouldn't need to update all the paths to the Images, but rather just the import statement to the a.class, however I usually just have a folder dubbed images which I dump them all into.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    The girl? :rolleyes:
    The term "boy" is very denigrating and even has a racist connotation in some cultures.

    Jos

Similar Threads

  1. Couple questions about Java
    By theadolescent in forum New To Java
    Replies: 3
    Last Post: 05-19-2010, 03:13 AM
  2. Couple Questions on Thread
    By Lil_Aziz1 in forum Threads and Synchronization
    Replies: 5
    Last Post: 01-06-2010, 03:02 PM
  3. Couple of questions regarding threading
    By exernet in forum New To Java
    Replies: 1
    Last Post: 12-15-2009, 01:23 PM
  4. Couple of newbie questions
    By ananasman in forum New To Java
    Replies: 11
    Last Post: 11-21-2008, 12:54 AM
  5. Replies: 9
    Last Post: 10-18-2008, 05:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •