A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
How to bat and jarTurning your program into an executable double-click program without the DOS command line. |
On Tuesday, March 16th 2004 at 01:39 PM By Simon Scott (View Profile) ![]() ![]() ![]() ![]() (Rated 4.4 with 15 votes) |
||
|
Out of all the questions posed in java among the most frequently asked has to be: 1. Copy, Paste and Compile these 2 Programs
2. How to create an instant click *.bat fileActually this one is very easy to do. All that is required here is that you open a simple text editor and then type in exactly the same thing as you would type in the DOS command line ie:- "java HelloWorld1" ![]() and then save this as "whateverNameYouWant.bat". From here you can now 'double click' on the icon that looks like this in the file named MS-DOS Batch File below and the java file will open. ![]() and you can also change the icon by right clicking the icon (*NB* This is how it works for Windows '98) and then click on 'properties' --- > 'program' --- > and then 'Change Icon...' In this case a light bulb icon as shown below. Once you have the short-cut icon you can drag or copy this to the desktop or wherever you want, though note that the java class file(s) must be in the same folder as the *.bat file for this to work. You will also realise that a black DOS screen Window opens when the bat file is clicked upon, this can be useful to capture messages for the user or for your own debugging messages -however, here's a little known and neat trick;- line 1: @echo OFF line 2: javaw MyApplication (done, save it again as: xxx.bat) The black screen is a feature that does not occur at all with the executable jar file and this is something you can use to bundle all of your files together; images, sounds, class files etc for a really professional finish. This is explained in part two of this tutorial, please click on the link: 'next page' below. ![]() Part 2: How to jarJar is an acronym for Java Archive file format, a file type that enables you to bundle multiple files into a single program. First we will make a jar for program 1, 'HelloWorld1.java'. Make sure that this compiles and runs in the usual way with the MS-DOS command. In order to make our jar the first thing we need to do is to write a Manifest File, this is a simple one line of text which will tell the JRE (Java Runtime Environment) interpreter where to find the java program with the main method signature. Write this in a simple text editor such as Notepad exactly the same as shown here: ![]() Note the following: Main-Class: - though not case sensitive, this must have a hyphen and must have a colon. Include a space: - you MUST include a space after the colon or you will get an error. The java file: - the file name is the java file with the main method signature, eg "public static void main(String []args){" (NB: do not include the '*.class' or '*.java in the name) Case sensitivity: - remember that java is case sensitive so the file is 'HelloWorld1' and not 'helloworld1'. Hit the enter key: - after the class file name (HelloWorld1), YOU MUST hit the Care when saving: - make sure the file is saved with an *.mf extension and not: Whatever.mf.txt as Notepad can sometimes have the annoying habit of associating any file extension not recognised with an additional '.txt'. The easiest way to ensure this is to use quotation marks (in Notepad only) when you save the file as shown above (Small techinical point: saving with an *.mft extension also works). Next: go to MS-DOS and type this in the command line: ![]() Lets go through that one at a time;- 1. First, be aware that "C:\Jar>" is merely the folder (named Jar) on the C-Drive where these files have been saved. You can save it wherever you wish, though having a seperate folder for your java files and another for your jar files is probably a good idea. 2. "jar": calls upon the jar command in the JDK or SDK. 3. "cmf": First 'c' means create a new and empty archive file for the jar file. Next 'm' means include a manifest file which is specified. Finally, 'f' specifies the named jar file to process, in this case 'Hello1.jar' (Note: for more information about these jar options see the Sun javadocs or for general information and further reading about jar files try one of the Sun tutorials on the subject) 4. "Hello1.mf": this is the name of the manifest file that we saved above. 5. "Hello1.jar": this is the specified name of the jar file that we will create. 6. "HelloWorld1.class": the compiled java class file to include. 7. Hit the Program 2:As stated earlier it is a good idea to create seperate folders for you various java or programming projects and this includes a folder for your jar files. In the case of program 2 there is more than one class file and one other file too, a gif file so the first thing we do here is to isolate them all in a single folder as you will see why in a moment. ![]() Remember that we will need a Manifest File as before, so open a simple text editor and type;- Main-Class: HelloWorld2 (and remember to press the ... and then save this as 'Hello2.mf' (or some other suitable name) in the same folder as the other files. Next the DOS Command in this case will be as follows: ![]() Note that here we simply write *.class, ie: include all of the class files in this folder and we could have also done the same with the gif file (ie, we could have written *.gif instead of Hi.gif) or any other of the file formats we might include, for instance *.wav files, *.jpg files, *.au files or whatever. Here is the final outcome of 'Hello2.jar' after double clicking the newly created icon. The advantages of the jar file over and above the bat file are basically two-fold. In the first place you don't get the ugly black DOS window rearing its head as your program executes and secondly all of the files: the class files, image data, audio data etc are bundled together with the jar file into one program. In fact you can try this, drag the 'Executable Jar File' to another folder or the desk top on its own and you will discover the independent wonders of our jar! If there is a downside to all this, then its the look of the default icon that the jar creates which has the look of a text file in my opinion, do not despair in this as you can easily create a great looking shortcut icon in the same way as we did with out bat file and with the assistance of other programs we can in fact do more (read on). Executable java programs: some alternative options.The other common solution is to turn our jar file into a Windows-based *.exe file. Before we go on, I feel somewhat obliged to point out that many java purists disapprove of such an approach and with some justification too. The point being (and it is a good one) is that java is a cross-platform, Operating System independent language and this is right at the heart of the java philosophy, so why change your solution to a narrow, platform specific OS such as a Windows *.exe file? Conversely there may be good reasons for this, for instance the Windows OS has a near monopoly for privately owned home PC's anyway, so should we worry. There is also the view that "java programs are slow" and I would like to say something about this;-
As with most things such matters boil down to individual choice, my personal preference is use a bat file for development (the test phase) and a jar file for distribution. Here in brief are your free options for converting your java file to an exe file 1. Duckware: allows you to package a Java application, along with its resources (like GIF, JPG, JNI DLLs, etc), into a single compressed 32-bit Windows EXE that targets multiple Java runtime environments (though this is not the same as a true exe file). 2. The GNU Project: is an Open-Source project is similar to the above that will convert java byte code to native machine code. Also highly recommended is Zerog as is the evaluation, trial versions Excelsior Jet and Native J: which will produce a pure exe file. The only ones that I've personally used is Duckware and Excelsior and with mixed results, but don't let that put you off. 3. Windows J++. Shortly after the release of java, Windows came up with their own (proprietary) version of java called J++. This probably caused more confusion than improvement to the cause of good programming practices, though the solution is still available if you're willing to work with java1 only. These days Microsoft are pushing their .NET platform and the C# language, which is a step in a better direction. Follow the link here to learn more of Microsofts own conversion tools. 4. Wrap it in C. Here is one for C programmers, the code below goes into the JNI (Java Native Interface) dll:
Note that the Win32Native.class is here for demo and testing purposes. This call to 'createProcess' would call cmd.exe passing the /c param and the path to the java compiler (javac.exe) and the path to the java files we wish to compile (c:\\*.java). The value returned is equal to what GetLastError() in the sytem would return. It is beyond the scope of this article to provide full lessons in the C language. The purpose here is merely to illustrate that C (and C++ for that matter) can provide useful alternatives as a solution to providing for executable click to start java files and the above solution does not invoke the DOS black window either. If you are interested in this root, then a new language will need to be at least partly learnt and there are various free compilers available such as here or here. And at the risk of delivering an information overload, here is a C Wrapper application at this link here (and credit to Sun JDC member alias 'DevWizard'). As much as I do like executable jar files, this is a solution that I personally like very much simply because you can specify your icon independently of what is saved on the user hard drive as an *.ico file. Try it, its a nice one and really easy to do, just follow the instructions on the word docs in the zip file (and there we have it, all my tricks and secrets are revealed). Finally, there was an interesting solution (supported by a number of big league players: IBM, SuSE, Apache, Borland and others) that came up with the Eclipse Project and what they termed as SWT (Standard Widget Toolkit) that used native widgets (ie: buttons, text areas, menu bars etc) to compile java-based code into native bytecode for desktop application development. A very good idea that provides yet another very credible alternative. Whatever solution you come up with remember that there are many, many potential solutions and the jar as a solution is a very good one in itself, some might say at least as good as any other. The purpose of the tutorials has been primarily to provide instructions so that you will now know how to bat and jar; - happy clicking! |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsBeatiful article.
iam very pleased with your web site
and thanks for providing such kind of
information
i would to ask it is possible to add applet to a jar fil
and possible to access by double clicking
if so please send me the source code to my email add
thanks.
bishnu
Thanks bishu,
though I think that applets work in a different way than other applications and you should be able to double-click them into action though the browser from the html page
Excellent article, very detailed, exactly what I was looking for.
Thanks!
Excelelnt article. Thanks Simon for provinding this information.
Regards,
---Nagesh
For those using anything later than Java 1.1, the show() command in HelloWorld2.java has been deprecated. Replace with setVisible(true);. That should work for you.
really avery nice article....
Very Nice Article, Detailed Step-By-Step Instructions....unlike MOST places. Good Job!
Wonderful article!
thanks, very useful!
sdasd
Excellent article!
However, when I moved the Hello2.jar file to another directory, it can no longer find the Hi.gif file - apparently it has to be in the same folder as the Hello2.jar file. I did include "Hi.gif" when creating the jar file.
Anyone who knows what is wrong?
(I am using Windows XP)
HOW CAN I GENERATE A FILE .JAR FROM .EXE OR .BAT.
THINKS.
HOW CAN I GENERATE A FILE .JAR FROM .EXE OR .BAT.
THINKS.
Related Tutorials
Related Source Code
Java Job Search