Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Making .JAR Files (Java Archives)
Index -> Programming, Java -> Java Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
the_short1




PostPosted: Sat Jun 24, 2006 12:25 pm   Post subject: [Tutorial] Making .JAR Files (Java Archives)

Here is brief tutorial on how to make Java archive's.

Notes:

- .jar Files can be run on any machine with a JRE installed, JRE is Java Runtimes, and they are on most computers, for running applets on webpages
- to create a jar you need a JDK (java development kit)
- you DONT need to include the source (.java) files
- you need COMPILED code (.class) files
- you need access to the command line, or create a .bat file
- .jar files are just glorified .zip files, you can open/extract files using any common zipping program, eg. Winrar

Steps:

1)Compile your project / all source required. Hint: copy all your .java files into a new folder, then compile them, it will create all the new class files required, and will be clean of extras that may be residing in the folder.

2)Folder should ONLY contain .class files, and mainClass.txt file

3)download the file "mainClass.txt" and store it with those class files, there is 2 lines in this text file, the first one contains the class you want run when you open the jar (eg: Main-Class: Pegs) the second one is a blank line (by pressing enter after Pegs) <IMPORTANT>

4)use the following command in a command prompt, or add it to a batch file (use notepad and save as .bat), then just double click batch file to run
without quotes "c:\j2sdk1.4.2_09\bin\jar cmvf c:\Pegged\mainClass.txt Pegged.jar c:\Pegged\*.class"

<change paths to match your version/directory structure>

-first argument is the location of the jdk (with jar creator program)
-second arguments to specify directory, custom main class, outputs files added, and f i forgot what it does..
-third argument is location of that custom main class text file
-fouth argument is name of the output jar file, it outputs to same location that the batch file (or current path in command line) is run from.
-fifth is the location of the class files you want to add to the archive, *.class is a wildcard for all class files

EDIT: Dont include your pictures/data files in the jar archive, you will get a file not found error, rather, create the jar with only your class files, and include that .jar and your data folder in a zip/rar archive.

For more info: http://java.sun.com/docs/books/tutorial/deployment/jar/build.html

Hope that helps.
-kevin



createJar.txt
 Description:
This is what my .bat file looks like, i store it in the same folder as my class files so my paths are all relative...

Download
 Filename:  createJar.txt
 Filesize:  60 Bytes
 Downloaded:  1740 Time(s)


mainClass.txt
 Description:
save in same folder as your classes, the blank line at the bottom is IMPORTANT!

Download
 Filename:  mainClass.txt
 Filesize:  20 Bytes
 Downloaded:  1544 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Fri Jun 30, 2006 4:35 pm   Post subject: (No subject)

I get an error when I run my jar file. It says that it cannot find the main class, even though I have all my classes in it, and I stated which class I wanted to run when I made the file. Am I doing something wrong? Btw, my JDK file is named j2sdk1.4.2_08. Would that have anything to do with it?
the_short1




PostPosted: Tue Jul 11, 2006 2:09 pm   Post subject: (No subject)

HellblazerX wrote:
I get an error when I run my jar file. It says that it cannot find the main class, even though I have all my classes in it, and I stated which class I wanted to run when I made the file. Am I doing something wrong? Btw, my JDK file is named j2sdk1.4.2_08. Would that have anything to do with it?


as long as you change the path to the JDK directory (first argument), version doesnt mater.

try storing the mainClass.txt file in the same folder as your .class files, as well in mainClass.txt you only need the class name, dont add '.class'. Ensure all your classes were added to the jar by using something like winrar or winzip to open it. And you remembered to keep that blank line at the end of the file right? (Main-Class: Pegs <press enter here>)

Hope that solves your problem.
-kevin
Carey




PostPosted: Fri Dec 15, 2006 11:57 am   Post subject: HELP!!!

i've got a problem. i can't run my .jar programs. when i click on them it opens up with my winace(like winrar). i've tried unassociating the .jar files in winace but it still opens up with them even when i restart my pc. is there a file in the .jar archive that you can run when winace opens it up to run the app (like a .exe or something)?

please help me!

thanks in advance!
the_short1




PostPosted: Mon Dec 18, 2006 11:39 am   Post subject: (No subject)

Nope... a .jar is just an archive.. like .zip ... it just holds all the .class files, etc. together. You can create a batch file to start the program... type this in notepad and save as "starter.bat" (ensure the drop down is "all files" not "Text File")

Start [Path To Java SDK]\bin\java -jar <myjarfile.jar>

where [Path to JAVA SDK] is probally "c:\j2sdk1.43_09" or similar

Also if you go to the "tools" menu then "folder options" (when browsing any folder)... you can manually remove the winace association.. (and change it to open using the java program (java.exe) and that should work too... please note.. if you create the batch file, it will ONLY work for your computer as not everyone has the same path to JSDK (or some only have JRE)

but then again.. . end users dont always have problems opening Jar files Smile
Good Luck -Kevin
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: