Java executable


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Java executable

  1. #1
    Join Date
    Nov 2002
    Posts
    9

    Java executable

    I have some applications (not applets) that I want to make into executables. How do I do this? I'm really tired of running my apps in dos and I dont like the whole applet idea. Thanks.

  2. #2
    Join Date
    Sep 2002
    Posts
    78
    well it takes a lot of work to make it into a (windows) executable but what i usually do is write batch files instead compiling in the dos prompt a sample batch file could look like this:

    Code:
    @echo off
    cd..
    cd my_application
    javac calc.java
    pause
    java calc
    first line does not display the commands typed by the batch files, only runs the commands
    second line just takes you down one level in your directory files, usually in windows u start in the folder c:/windows/ so the cd.. makes u go to c:
    third line then goes to the folder my_application (change this to whatever your folder is called)
    fourth line compiles yoru code
    fifth line types "please press a key to continue" on screen and waits for user input
    fifght line runs your java application

    type this code in notepad or some such text editor and be sure to safe it as "filename.bat" dont forget the quotes if you use notepad.

    good luck and hope this was some help
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

  3. #3
    Join Date
    Nov 2002
    Posts
    9
    nice nice, I see what you mean. so the batch file would be doing everything the user would normally have to do. That is an excellent an idea. But I wouldnt be able to run this batch file from just any folder, it would have to be the bin correct? I ask this because if I try to javac within another folder, i cant do it. But I would be able to java it from another location correct? So in my batch would I put the javac command or can i just put "java prog name" as long as the java class file is in the folder?

  4. #4
    Join Date
    Sep 2002
    Posts
    78
    have you changed your autoexec.bat file to include the path to your java compiler? your autoexec.bat file is usually just in your c drive (not in a folder). if u dont have a autoexec.bat file then you should make one and inside it add a line like this:
    Code:
    path=%path%;c:\j2sdk1.4.1\bin
    in my case my java compiler is in teh folder j2sdk1.4.1, change this to wherever your compiler is located. also when you change your autoexec.bat file you have to restart before it takes effect. when you have restarted you can call javac from any folder in your computer.

    good luck
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

  5. #5
    Join Date
    Nov 2002
    Posts
    3
    even better than a batch file is an executable Jar file.

  6. #6
    Join Date
    Nov 2002
    Posts
    138

    JAR

    and if anyone didn't tell you how to make an Executable Jar file, or you dont' know how to make one, it's like this:
    @ the command prompt type:
    jar -cfm <name of output jar file> <name of manifest file> <classes or anything to include>
    there are spaces, ok, between each argument.
    - the output jar file is any name you want your jar file to be.
    -the manifest file is the file which contains the name of the class which contains the main method. just make a file of any name with this in it: Main-Class: <name of class with main method>
    -the last one in this example is what you want in the jar file. you could do *.class or *.*, and jar will automatically include all classes in current folder,or all files, respectively. or you could specify MyClass.class, etc., that depends on you.
    Actually, you could do more with jar files, than just make them execute your java program without a dos command window opening up everytime, and making your desktop look cluttered.

  7. #7
    Join Date
    Nov 2002
    Posts
    138
    java is case sensitive so... you know what i mean....

  8. #8
    Join Date
    Nov 2002
    Posts
    138
    java is case sensitive so... you know what i mean....

  9. #9
    Join Date
    Nov 2002
    Posts
    4
    Hello,
    I created a jar file from one of my programs... When i try to open it using "javaw" or whatever.. it Says "Could not find main class." What is wrong? I am also comfused by when you say "manifest"... What does that mean? Thanks.

  10. #10
    Join Date
    Nov 2002
    Posts
    9
    I created a jar, but i did it this way

    jar cf nameOFprog.jar nameOFprog.java *.class and it created the jar file. I tried it the way you told me, and it said i had an error that looked like this
    java.io.IOException: invalid header field
    at java.util.jar.Attributes.read(Attributes.java:351)
    at java.util.jar.Manifest.read(Manifest.java:162)
    at java.util.jar.Manifest.<init>(Manifest.java:52)
    at sun.tools.jar.Main.run(Main.java:124)
    at sun.tools.jar.Main.main(Main.java:904)

    I have my jar file now, but it wont open when i double click on it. what is wrong?

  11. #11
    Join Date
    Sep 2002
    Posts
    124
    Hi, all

    There is a free ( personal use only ) Java compiler out there. I've not used it yet, so can't really comment much. Besides, the whole point with Java is its portability but if anyone wants to try it.....

    http://www.excelsior-usa.com/jet.html


    Regards,
    Joyous Monkey

  12. #12
    Join Date
    Nov 2002
    Posts
    138
    =) you had it all wrong. you must have a MANIFEST file so that java can execute your program.

    it's ---> jar -cfm <output file> <manifest file> <files to include in jar>
    e.g. jar -cfm MyProgram.jar Manifest *.class

    don't forget the m coz it tells jar that you are including a manifest file.

    <output file> must have jar as an extension or it won't run

    manifest is simple. just open any text editor program and place this inside:

    Main-Class: <name of class with the main method>

    don't place the .class extension in the name of the class file with the main method. if the class is Main.class, jsut place Main. only the class with the main method is required in the manifest file. no other class is needed.
    if the Main-Class ( with first letters in capital ) keyword is not being accepted, try Main-class ( this is with a small letter c in class ) or main-class. i am not so sure anymore coz i haven't used jar in quite a while already. so just experiment until it comes out ok. ok? but the first thing that comes into mind is really Main-Class. then save it with any name. i don't ust an extension. i just type in a name like Manifest or one that's associated to a program i making a jar file for.

    also, you don't have to include your java source codes ( *.java files ) in a jar file for it to be executable. all it needs to become an executable jar file are the class files. all class files that a particular program needs, so be sure everything is in the jar file. try *.class (wildcards) so you won't have to enumerate all the classes.

    again java is case sensitive so make sure you get the names correctly.

    and to open a jar file just click the icon of your jar file. you need not open a command prompt. if you want to open a jar file in DOS, don't use javaw. instead, use java -jar <name of jar file>.

    hope this helps you.....

  13. #13
    Join Date
    Nov 2002
    Posts
    138

    Talking to Joyous Monkey

    got a nice nick for a moderator. i thought moderators had serious nicks.

    anyway, ya, java's edge over other langs is it's portability. but the thread starter was saying something like he's tired of always opening DOS and typing java **** to start a program. so using a batch file or a jar file is one way of doing away with the DOS prompt, etc.

  14. #14
    Join Date
    Nov 2002
    Posts
    138

    Talking to Joyous Monkey

    got a nice nick for a moderator. i thought moderators had serious nicks.

    anyway, ya, java's edge over other langs is it's portability. but the thread starter was saying something like he's tired of always opening DOS and typing java **** to start a program. so using a batch file or a jar file is one way of doing away with the DOS prompt, etc.

  15. #15
    Join Date
    Nov 2002
    Posts
    9
    thx for all suggestions. here is the way to make a jar executable for anyone who has some interset. This way works for sure. 100%:

    jar -cmf manifest.mf name_of_prog.jar *.class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center