Results 1 to 6 of 6
- 11-10-2012, 11:20 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
How to create a jar file without changing the development directory structure
Hi,
I'm attempting to figure out creating jar files of programs and have a problem.
Given the following directory structure:
MyProgram
....bin (class files for myprogram)
........mypackage
............myprogram.class (package mypackage;)
....images (images used by myprogram)
........myimage01.jpg
........myimage02.jpg
....src (source files for myprogram)
........mypackage
............myprogram.java (package mypackage;)
...........(accesses the image files as "images.myimage01.jpg")
What is the best way to create a jar file for this program in DOS not thru eclipse or any other tool?
I have tried every combination of
at what level to attempt it;
where to place the manifest file;
how to format the name for the main class in the manifest file (Main-Class: mypackage.myprogram or bin.mypackage.myprogram)
how to incorporate the image directory;
The only way I have succeeded is to do the following:
create the following directory with the following contents
....jarcreation
........images
............myimage01.jpg
............myimage02.jpg
........myprogram.java
........manifest.txt (Main-Class: myprogram)
modify myprogram.java
....remove the package statement;
cd in DOS to directory jarcreation
compile myprogram: jarcreation>javac myprogram.java so there's a class file in the directory
call jarcreation>jar cvmf manifest.txt myprogram.jar *
Now I have a jar file that I can run with:
....jarcreation> java -jar myprogram.jar
I believe the top directory structure is the accepted standard structure for a Java program so isn't there a way to create a jar file from that structure?
Am I missing something about the jar command?
Please be very specific about
what directory I should be at in DOS;
how the main class should be named in the manifest file;
where the manifest file should go;
how the main class should be named in the jar creation command;
how to include the Images directory.
Thanks for the help.Last edited by msphelix; 11-10-2012 at 11:30 PM.
- 11-13-2012, 05:38 PM #2
Re: How to create a jar file without changing the development directory structure
Have you created a manifest file? This might help: Creating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
- 11-17-2012, 11:08 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: How to create a jar file without changing the development directory structure
Post 2
Thanks, but that is not the issue here. I can take the the original directory structure and remove it from the bin and src directories so the directory structure is as follows:
MyProgram
....images (images used by myprogram)
........myimage01.jpg
........myimage02.jpg
....mypackage
........myprogram.class
........myprogram.java (package mypackage;)
........(accesses the image files as "images.myimage01.jpg")
....MyProgramManifest.txt
MyProgramManifest.txt contains this line:
Main-Class: mypackage.myprogram
and it works fine.
It's when I separate the class and java files into separate directories, bin and src, that I can't figure out where to place the manifest file and what it should say.
With the original directory structure in the original post, I have tried
saying Main:Class: bin.mypackage.myprogram in the manifest file and
moving the original manifest file to bin and creating the jar file there.
Can you or anyone tell me a way to do what I want?
Also, am I wrong in thinking that, once I have created a working jar file, MyProgram.jar, that correctly displays the image files in images when run from the MyProgram directory, I should be able to copy MyProgram.jar to a different directory and run it from there? If MyProgram.jar should work in any directory, then I have another problem - MyProgram.jar will run fine but does not display the images. Any ideas on that issue?Last edited by msphelix; 11-18-2012 at 10:55 PM.
- 11-18-2012, 01:15 PM #4
Re: How to create a jar file without changing the development directory structure
If your manifest file is really called "MyProgramManifest.txt" then it wont work at all. See Understanding the Default Manifest (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
You can include your source files, that's not a problem. The jar just ignores them at runtime. You probably aren't referencing your images and whatnot within your program correctly. If your package declared in the code or did you just drop your code in a folder?
- 11-18-2012, 11:01 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: How to create a jar file without changing the development directory structure
Post 3
Thank you for replying.
1.
Why is calling my manifest file MyProgram.txt wrong? See Setting an Application's Entry Point http://docs.oracle.com/javase/tutori...ar/appman.html which clearly says:
"
We first create a text file named Manifest.txt with the following contents:
Main-Class: MyPackage.MyClass
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
"
This is exactly what I am doing.
Maybe I wasn't clear enough -see my post 2 from yesterday:
with the directory structure there, including the MyProgramManifest.txt file, I can create jar file MyProgram.jar from the MyProgram directory using the following command:
MyProgram>jar -cvmf MyProgramManifest.txt MyProgram.jar Images mypackage/ *.class
and can run it from the MyProgram directory using the following command:
MyProgram>java -jar MyProgram.jar
I was trying to figure out how to do this with the directory structure from my original post which separates the class files and java source files into separate directories, bin and src. I'm beginning to believe that it's not possible to do it from that directory structure.
2.
I guessed that I'm not referencing the images correctly. As is shown in the directory structure in my second post, the one without the bin and src directories, the images are in a directory called Images at the same level as the directory mypackage which contains the source and class files. The class are in the mypackage package and MainPanel references the image files as new ImageIcon("images\\myimage01.jpg");
This does work when I am running MyProgram.jar from the MyProgram directory but if I copy it into any other directory, it runs but the images do not display.
Is there a way to reference the images so the jar file works whereever I put it?Last edited by msphelix; 11-18-2012 at 11:07 PM.
- 11-19-2012, 02:50 AM #6
Similar Threads
-
How to create a zip out file from the current directory of input files
By renu in forum New To JavaReplies: 5Last Post: 11-24-2010, 10:57 AM -
Zip Directory Structure
By freestatedon in forum New To JavaReplies: 2Last Post: 02-15-2010, 06:11 AM -
Directory Structure
By rummy in forum New To JavaReplies: 1Last Post: 01-21-2010, 12:03 PM -
Recreate directory structure when extracting from zip file
By jimmy in forum New To JavaReplies: 0Last Post: 03-18-2009, 01:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks