Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How can store image/jar file in my prgram ?

store image

  • Please log in to reply
3 replies to this topic

#1 xxxxjayxxx

xxxxjayxxx

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 123 posts

Posted 04 August 2011 - 07:36 AM

Which means that when I open my program in another computer, I do not need to import image/jar files inorder for the program to work properply ?
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 05 August 2011 - 06:08 AM

You'll need to package your program in a .jar file along with the images you wish to include. If you're using NetBeans, simply adding the image to a package in your project takes care of this automatically. Alternatively, you can use the JAR command line utility to package your object files and image files, along with any other resources you wish to include.

Docs on the JAR command line utility:
jar-The Java Archive Tool

In your code, you can access the image using the following:
Image img = Toolkit.getDefaultToolkit().getImage(MyClass.class.getResource("image.jpg"));
where MyClass is the name of your application class and image.jpg is the filename of your image, obviously. If the image isn't located in the same package as your class, you'll have to provide the absolute path in the filename string.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 xxxxjayxxx

xxxxjayxxx

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 123 posts

Posted 05 August 2011 - 07:37 AM

Sorry what I meant was can I don't import the image as in ...

Is there any way that I do not need to import the image and it saved in my jar file.

Which means that when I open the executable jar file, every images are in place so that I do not need to import anything for another comp user.

Hope you get what I'm trying to say.
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 05 August 2011 - 11:06 AM

I don't understand what you're asking. What do you mean by:

every images are in place so that I do not need to import anything for another comp user.


Your code will have to load your image files from somewhere in order to be able to run on another computer, so you'll have to transport those images to the other computer somehow.

The way resources (image files are just one kind of resource) are commonly distributed is by wrapping them up with your application's object code in a JAR file (Java ARchive). This is so you don't have to copy all the resource files separately and hope none of them get moved or deleted. Your Java Archive file keeps them all neatly packaged so your application's code knows just where to locate them.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: store image

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download