Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (woodrow73):

Making jar files?

OpenStudy (woodrow73):

I have a few programs that have multiple classes, and .txt as well as .jpeg files that it uses. Though by making an all encompassing jar file - it would be nice for program portability. I'm on windows - anybody know a good way to go about this? Preferably without IDE assistance; just cmd.

OpenStudy (p0sitr0n):

this would be the basic command jar cf jar-file input-file(s) for more info, https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

OpenStudy (woodrow73):

Thanks for the link

OpenStudy (e.mccormick):

Also, if you use an IDE it may have some packaging tools to help you.

OpenStudy (anonymous):

If you store resources in the JAR file you can use class.getResource. Spend some time reading the documentation and discussions online to learn how to do that.

OpenStudy (woodrow73):

So far I've found out how to make a jar file using the cmd-- (Just saw a video of making jar files within an ide, i'll try that soon for sure) https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html however making the jar wasn't simple as pie. For me, using the 'cf' didn't work; instead I used 'cfm', and I had to add the name of a .txt file containing the Main class file name immediately after I list the name of the jar file that I'm going to create within .txt file which holds main class name I put this: ``` Main-Class: HelloWorld ``` that followed by 2 enter keys (not sure if necessary). I'll add a picture of me trying 'cf' & failing, then using 'cfm' and making it successfully below. So for me it was this style; ``` syntax I entered in the cmd (ignore parenthesis & imagine all on 1 line): jar cfm (file name that will be created with .jar extension (add .jar extension)) (name of file which holds the name of the main class (in same syntax as I listed above) to .txt extension) (input file(s)) (more files) (a java file here) (a class file here) (a .jpg file here) (a .txt file here) ``` & wa-la, I have a jar file - and the program can be run either by clicking on it, or typing this in the cmd (assuming it's in the right directory): ``` java -jar (jar file name (add .jar extension)) ``` Here is the video tutorial I followed for this method: https://www.youtube.com/watch?v=WTfjbOIfi10 In the picture- note that the 'manifest attribute' refers to the .txt document in this format (as I showed above): ``` Main-Class: HelloWorld ``` that followed by 2 enter keys (not sure if necessary). & note that it doesn't have to be named "manifest.txt", can be a programmer-defined-name. I'm sure this would all make better sense to me if I knew a bit more about windows shell scripting.

OpenStudy (woodrow73):

This is great! Extreme convenience-- trying to transfer a java program with 30 jpg's and .txt files is extremely inefficient; that's where jar comes in - all in one file. So, how would one go about posting a jar file online / sending it to someone else? Didn't see any easy solutions on google-- and would rather not have to upload to a 'sketchy' site lest it gives viruses.

OpenStudy (anonymous):

Dropbox

OpenStudy (anonymous):

Or if it is small enough an email attachment I suppose?

OpenStudy (woodrow73):

Thanks for the tips @Alchemista Though unfortunately the 'resources' aren't attatched to the jar file. In the example picture I posted above- even though I added "read.txt" in the cmd line, it didn't 'register' so if I move the jar file to a different location (like desktop where "read.txt" doesn't exist) then the program will throw an IOException- So I would have to send the .txt file & resources separate from the jar file -- if I then added a file called "read.txt" on the desktop, then the jar file would run again. I'll try sending a file containing both the resources and the jar file and see if that works

OpenStudy (anonymous):

As I mentioned earlier you need to use the class.getResource API to access a resource within the JAR file. You also probably need to be careful with how you include the resource in the JAR via command line. I think in the end you will probably have a lot less trouble if you just use Eclipse.

OpenStudy (woodrow73):

``` InputStream inputstream = getClass().getResourceAsStream("/read.txt"); InputStreamReader readstream = new InputStreamReader(inputstream); //read.txt contains "Hello World" on the first line BufferedReader in = new BufferedReader(readstream); ``` after attatching read.txt to the jar file -- I had to change how I read from file "read.txt", into the code you see above. (has to be in a non-static location for getClass() to compile) @Alchemista you're definitely right about the ide & it's many eases; though I like the practice of doing both.

OpenStudy (woodrow73):

And in terms of being able to get an image from a jar file from your source code in the jar file -- I found this method of turning that jpg into an ImageIcon object: ``` //usual applet imports import javax.swing.*; import java.awt.*; ImageIcon ic = new ImageIcon(getClass().getResource("/hub.jpg")); //if you care to know how to display that ic in a JFrame; JFrame jf = new JFrame("Image Display"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); JLabel lab = new JLabel(ic); //add imageicon obj to jlabel's constructor jp.add(lab); //add label to jpanel through add method jf.add(jp); //add panel to jframe through add method jf.pack(); //set size to tightly hug the image jf.setVisible(true); ``` if you don't care about .jar, then you can extract an image by specifying the path in the ImageIcon constructor (with proper extension); ``` ImageIcon ic = new ImageIcon("C:\\thisfolder\\inhere\\show.jpg"); ``` I've noticed it can be picky between jpg and jpeg. -It's much like specifying the path of a .txt file.

OpenStudy (woodrow73):

At my current level.. I think I only need to know how to access .txt and .jpg files from a jar.

OpenStudy (lyrae):

When your (java) projects starts to get big and have lots of dependencies and resources it's usually a good idea to start using project management and comprehension tools like Maven or Gradle. They help you with project/file organization, dependencie/RC management, compilation, packaging and distribution. You'll find these in most professionnel environments and they really do help although they might seem intimidating at first.

OpenStudy (woodrow73):

Many thanks for the suggestion-- I look forward to testing one of them out.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!