.jar executable

Hi!
I have been trying to create an executable jar, but I only get the message "Could not find main class. Program will exit".
This is what I have done!
1. I created a java file that prints "Hello". This is the one
import java.io.*;
public class Test{
     public static void main (String [] args) throws IOException{
          System.out.println("Hello");
2. I compile it
3. I make a jar of the two files. The java file and the class file. To do so I type the following in the command window:
jar cf temp.jar Test.java Test.class
4. I create a manifest file and name it Manifest.txt. It contains the text:
Main-Class: temp.Test
5. I now create the (expected) executable jar file. Typing the following in the command window:
jar cfm MyJar.jar Manifest.txt temp.jar
And when I now try to double click the MyJar.jar I only get "Could not find main class. Program will exit". WHAT am I doing wrong??? Please help!
/Jonathan

jonfra wrote:
Hi!
I have been trying to create an executable jar, but I only get the message "Could not find main class. Program will exit".
This is what I have done!
1. I created a java file that prints "Hello". This is the one
import java.io.*;
public class Test{
     public static void main (String [] args) throws IOException{
          System.out.println("Hello");
First off, you know that you don't need to import java.io or throw IOException for what you have here, okay?
2. I compile it
How did you compile it? As >javac Test.java? ... or >javac -classpath . Test.java? ... or ?
3. I make a jar of the two files. The java file and the class file. To do so I type the following in the command window:
Not wrong to do it, but why do you want the source code in the jar file?
jar cf temp.jar Test.java Test.class
I'd skip this step '3' and move on to step 4.
4. I create a manifest file and name it Manifest.txt. It contains the text:
Main-Class: temp.TestMake sure that you have an EOL character at the end of Manifest.txt - like so:
Main-Class: temp.Test
- this line needs to be blank -
Also, why the temp. ? your class - as Mapleshaft alluded to, does not have a package statement.
>
5. I now create the (expected) executable jar file. Typing the following in the command window:
jar cfm MyJar.jar Manifest.txt temp.jar
After skipping step 3 entirely, jar as:
jar cmf0 Manifest.txt MyJar.jar Test.class
And when I now try to double click the MyJar.jar I only get "Could not find main class. Program will exit". WHAT am I doing wrong??? Please help!
/JonathanLastly, this is not a GUI, so double clicking on it won't do anything anyway. You'll need to run it from the command line as >java -jar MyJar.jar

Similar Messages

  • JAR Executable works in netbeans but not under Dos?

    Hi all,
    I have created a jar executable and it works fine in netbeans(it was not created in netbeans) but I can't get it to run on the command line. If I type java ClassName.MainClass it will run but not using the command java JarFile.jar or java -jar JarFile.jar
    Any help would be most appreciated.

    When creating the manifest file did you add a carriage return at the end of the manifest? e.g.
    Main-Class:Hello.class
    (carriage return here on new line)
    I think it runs into problems if you dont add a carriage return ( a new blank line after manifest).
    Hope this helps
    Riz

  • How to get a file in the same dir with the jar-executable

    Hi,
    I need to read/write a file that exists in the same directory with the jar-executable. How can i do that? I think that when i specify no path for the file, the file has to be in current path. (Is that right?)
    Note: I do not know in which directory the jar and the file are.
    Thanx in advance

    Hi,
    I need to read/write a file that exists in the same
    directory with the jar-executable. How can i do that?
    I think that when i specify no path for the file, the
    file has to be in current path. (Is that right?)
    Note: I do not know in which directory the jar and
    the file are.
    Thanx in advance
    When you specify no path for the file, the file has to be in the directory where the virtual machine was started. ( the directory the java command was invoked in .)
    If you can't control the directory in which the vm is started, but you know the name of the .jar file you can do the following. This trick takes advantage of the fact that if you are using classes in a jar file, the name of the jar file must be in your classpath.
    public static String getPathOfJar( String nameOfJar )
    throws Exception
    StringTokenizer st = new StringTokenizer(
    System.getProperty( "java.class.path" ) ,
              System.getProperty( "path.separator" ) );
    String jarfile = "";
    while ( st.hasMoreTokens() )     
    String token = st.nextToken();
    if ( token.indexOf( nameOfJar ) > -1 )
    jarfile = token;
    break;
    if ( jarfile.equals("") ) throw new Exception( "Jar not found in classpath" );
    String path = jarfile.substring( 0 , jarfile.indexOf( nameOfJar ) );
    return path;
    //To open a file in the same directory as the sun archive tools.jar
    File f = new File( getPathofJar( "tools.jar" ) + "someFile.txt" );

  • Sending Command Line Arguments to Jar Executable

    Hi All
    I wish to send Command Line Arguments to Jar Executable.
    i.e. before I Jar'd my program, I would do the following
    public class CmdLnArgmntExp {
    public static void main(String[] args) {
    System.out.println("d");
    for (int i = 0; i < args.length; i++)
    System.out.println(args);
    Running the program in Console.
    Java CmdLnArgmntExp arg1 arg2 arg3 arg4Output:
    arg1
    arg2
    arg3
    arg4
    How would I do this to a Jar executable, hope that made sense
    Thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    java -jar executable-jar-file-containing-class-CmdLnArgmntExp.jar arg1 arg2 arg3 arg4

  • Jar executable issue after jvm update

    I created a small demo application and bundled my classes and media files in a jar executable file. Everything was fine until I updated J2SE from 1.5.0 to 1.6.0_01.
    The application now starts, gets its input normally but soon after hangs, and the Swing GUI doesn't show up anymore.
    The jar file contains 4 packages (one of them contains the main class) and this simple manifest file:
    Main-Class: myapp.myAppLauncher
    Classpath: .\myapp.jar
    I created the jar as follows:
    jar cvfm gdv.jar Manifest.txt myapp mypackage2 mypackage3 mypackage4
    I recompiled the source before creating the jar archive, and if I launch the application from the console I see no difference from before. The problem is only for the jar version.

    Manually download and install the Java 4 update http://www.apple.com/downloads/macosx/apple/application_updates/javaformacosx105 update4.html, and Safari 4.0.1 http://www.apple.com/safari/download/ Run Software update, install anything that pop up, repair permissions, and restart. That should fix your problem.

  • How do I put my jar executables into a cab file to create a setup/install?

    I want to create a setup program for installing my jar executable. I've seen ones that create cab files with a setup.exe program and would like to do the same. Any suggestions?

    Well, JARs generally don't have support files. That's the whole idea. A JAR is designed to be a stand alone application. You don't really need to install it, for that matter. I just thought you might be wanted shortcuts or something.
    Actually, there is some way to install Java programs through Java Web Start, if you want to check that out. I don't know anything about it, though.

  • Double clicking jar executables

    Hi everyone
    I know that this topic has been discussed before - but I have scanned all discussions, and the problem hasn't been solved yet. There have been some good suggestions, but no definitive solution provided.
    I have a jar executable supplied to me by a commercial software company. The application is packaged in a jar file. I have checked that the manifest has the correct Main-Class association in it. I can install the jar on some Win 2000 copmuters and it executes fine by just double clicking on the jar file. On my laptop which also has Win 2000 OS it simply refuses to execute. The very frustrating JVM Launcher error message "Could not find main class. Program will exit." is displayed.
    The only way that I have found to get around it is to put a batch file in the same directory as the jar file containing "start /MAX javaw -jar "C:\Program Files\EqWave\eqwave181\eqwave.jar"" and execute this batch file from a shortcut placed on the desktop. Not a very good work-round - but at least it works.
    What I can't figure out is why the jar will work when double clicked on some computers, but not on my laptop. Obviously it has something to do with my laptop setup.I have reinstalled the latest jre-1_5_0-windows-i586.exe, but it still won't work.
    Please, please, pretty please - what am I doing wrong?????
    Cheers
    Mike T

    On your laptop the file association could be wrong : check if the file association with .jar is "javaw -jar" and not only "javaw".
    It looks like the "-jar" option is missing in the command field of your file association...
    On my Windows 2000 computer, the file association for .jar is :
    "C:\Program Files\Java\j2re1.4.1\bin\javaw.exe" -jar "%1" %*Modify your file association and I bet it will be better.

  • How to run a Jar executable with non standard JVM memory settings

    Hi,
    I have a jar-executable that needs more memory than the JVM uses by default.
    The application works with:
    java -Xms128M -Xmx256M myapp
    How can the jar know that it need to start like this?
    I found a solution here http://forum.java.sun.com/thread.jsp?forum=32&thread=188484 , but i don't want to do this with a shortcut.
    Is there anyway to do this?
    Thanx in advance.

    Well i'll do that:
    public class launch
      public static String getJarPath()
        String path="";
        try
          StringTokenizer st = new StringTokenizer(System.getProperty( "java.class.path" ) ,System.getProperty( "path.separator" ) );
          String jarfile = "";
          while ( st.hasMoreTokens() )
            String token = st.nextToken();
            if ( token.indexOf("launch.jar") > -1 )
               jarfile = token;
               break;
          if ( jarfile.equals("") ) throw new Exception( "Jar not found in classpath" );
          path = jarfile.substring( 0 , jarfile.indexOf("launch.jar"));
       catch (Exception e)
        new AlertWin(e.toString());
       finally
        return path;
      public static void main(String[] args)
        try
          String path=launch.getJarPath();
          Runtime.getRuntime().exec("javaw -Xms128M -Xmx256M -jar " + path + "toBeExecuted.jar");
        catch(IOException e)
          new AlertWin(e.toString());
    }The launch.jar has to be in the same dir with the toBeExecuted.jar. (ofcourse that could change and the code is much simpler).

  • How to install a jar executable as a Win XP pro NT Service

    hi folks.
    Got a .jar file (compiled it in JBuilder, archived it as "Application" it executes by double clicking it. It works by scheduling it through schdule tasks...
    However I would like to run the JAR executable as a Windows Service (Win XP Pro). Is this possible in java?
    I have done similar thing in .NET C#/vb.net but service creation/processing is written within the code. All you do is go to dos prompt and type net start ... etc.
    How can I do the same in java?
    Please assist.

    Got a .jar file (compiled it in JBuilder, archived it
    as "Application" it executes by double clicking it.
    It works by scheduling it through schdule tasks...
    However I would like to run the JAR executable as a
    Windows Service (Win XP Pro). Is this possible in
    java?It might be possible, but knowing anything about OS specific stuff is not what Java is intended for. There is a Runtime.exec() method provided for any OS - specific stuff.

  • Export jdeveloper as jar executable file

    hello, I have a simple application developed using jdeveloper which access an oracle database and retrieve data. I would like to export this application as jar executable file. how can I do this using jdeveloper.
    Best regards

    Add a manifest file that identifies the main class. If you have questions on how to do that using your IDE, I suggest you hit 'F1' or visit a forum that deals with that IDE.

  • Including images in jar executable files. Using images in jar files.

    Does anybody know what is the reason of my problem??
    I created a package called test in my eclipse env. and in that package I placed folder named graph in which there is a file Help.png.
    Here is all the code I run in eclipse and it runs properly. When I create a jar file (using eclipse wizard) to execute without eclipse it can not execute and shows nothing (process starts and I have to kill it by myself in OS). Maybe there is some mistake with making jar... I do not know
    So let someone try tu run my class or build jar file and try to execute and share his observations (solutions) ??
    package test;
    import java.awt.Toolkit;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    public class TestWindow extends JFrame {
         private JButton jButton1 = new JButton(new JARImage().getImage("Help.png"));
         public TestWindow() {
              this.getContentPane().add(jButton1);
              this.validate();
              this.setVisible(true);
         private class JARImage {
              protected ImageIcon getImage(String imageName) {
                   ImageIcon image = new ImageIcon();
                   try {
                        image.setImage((Toolkit.getDefaultToolkit().getImage(getClass()
                                  .getResource("graph/" + imageName))));
                   } catch (Exception ex) {
                        System.out.println("Image Error: " + ex);
                        ex.printStackTrace();
                   return image;
         public static void main(String[] args) {
              try {
                   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              } catch (Exception e) {
                   e.printStackTrace();
              new TestWindow();
    }

    Uncaught error fetching image:
    java.lang.NullPointerException
         at sun.awt.image.URLImageSource.getConnection(Unknown Source)
         at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
         at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
         at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
         at sun.awt.image.ImageFetcher.run(Unknown Source)

  • How to make jar executable?

    I am distributing jar file to the user, which is executed using a batch file.In the batch file following things are done:
    1. Setting the sytem variables
    2. lauching the jar file
    I want to avoid the usage of batch file as it is not safe.
    Is there any way to launch the jar file as an executable along with the system variables set?

    I am distributing jar file to the user, which is
    executed using a batch file.In the batch file
    following things are done:
    1. Setting the sytem variables
    2. lauching the jar file
    I want to avoid the usage of batch file as it is not
    safe.
    Is there any way to launch the jar file as an
    executable along with the system variables set?No, that can't be done. Do you really need the system variables? What are they used for?
    Kaj

  • Problems creating a jar executable file...

    Hi there, I am new to these forums, my name is Jay.
    I am trying to create an executable .jar file for my program I created in JBuilder 2005, with java files and several other files as well. I am having MAJOR problems with either the main class not being found or failing to load Main-Class manifest attribute.
    I tried to make a .txt with two lines, including the first with the MAIN-CLASS: className and stuff and a second line blank, and use the cmf command line to run it and include it in there. I ran it and created it no problem, but then I try to run it, and I get the error of the main class not being found and I have specified the correct directory and class name.
    Then I try to make my own manifest file with the correct directory and class name by making a txt file into an .mf file and when I try the cf command line this time, it gives me the error of failing to load Main-Class manifest attribute.
    I have even followed the tutorials on the Sun developer website including Manifest making and stuff and everything.
    I have worked on this program (java) for months and I am now STUCK. If someone could maybe give me some assistance or suggestions as to what my problem might be, I would greatly appreciate it so much. Or if you want, you can e-mail me, @ [email protected] with suggestions. Thank you all very much.
    I have a folder in a directory. It's called SuperPassword, which is the folder where my project files and EVERYTHING is. my .class files are in the classes/superpassword subdirectory of the folder SuperPassword, my java files are in the src/superpassword subdirectory and my manifest file and txt files are in the SuperPassword main directory. Maybe that could give you guys a bit of a hint as to what I am dealing with, I want to explain it as clearly as possible.
    Jay

    I have a folder in a directory. It's called
    SuperPassword, which is the folder where my project
    files and EVERYTHING is. my .class files are in the
    classes/superpassword subdirectory of the folder
    SuperPassword, my java files are in the
    src/superpassword subdirectory and my manifest file
    and txt files are in the SuperPassword main
    directory. Maybe that could give you guys a bit of a
    hint as to what I am dealing with, I want to explain
    it as clearly as possible.
    JaySo I guess you have a directory path SuperPassword\classes\superpassword and the .class files are inside. Are the classes in a superpassword package? If they are, then the Main-Class must be something like "superpassword.MainClass" and when you jar the .class files you need to make sure they are in a superpassword directory inside the jar - like "jar cmf thejar.jar themanifest.txt superpassword\*.class"
    You can check the directory structure by displaying the contents of the jar using "jar tf thejar.jar"
    Also, remember that java is case sensitive.

  • Java JAR executable

    I know this violates platform independence, but does anyone know of a tool to convert a jar that runs a class based on its manifest into an executable for a given platform (I assume such a tool would merge parts of the JRE with the jar itself)?

    Do a google search on Java to Exe. This will give you about 1.7 million hits. They have different ways of working so search until you find one that does what you want.

  • JAR executable do not find third party Jars

    Boy I am beat.
    I am trying to build executable jar that needs bunch of third party jars.
    So I import the jars into a ext folder at the top of the pkg. I jar it all up (using Ant)with a manifast that point the classpath to the jars at the ext folder
    Dir Structure:
    myJar.jar
    |
    +-build
    |main class
    |
    +EXT
    |
    + bunchOfJars
    </pre>
    Manifest-Version: 1.0
    Created-By: Apache Ant 1.5
    Built-By: Ted I Weitz
    Classpath: ext/dom.jar ext/xsltc.jar ext/dt jar ext/vecmath.jar
    ext/j3dcore.jar ext/j3dutils.jar ext/xercesImpl.jar ext/xalan.jar
    Main-Class: myMain
    Command:
    java -jar myJar.jar
    I will be very gratfull for any idea
    Ted

    Just posted on this in another thread I am watching. As far as I know, and every other post I have seen on this topic, there is no way to do this without using a custom classloader that is used by the executing .jar itself to load any .jar files. How I can see this work is the main class is nothing more than a "classloader" launcher. It creates a new instance of this custom classloader. It then opens "itself" (the .jar the main class and all the .jar's are in), reads the manifest file to locate the .jar files the application requires, then either loads them, or somehow is able to reference them without directly loading them. The URLClassLoader is a good one to extend for this. It already opens up any .jar file specified in its path when being created, scanning for any files in there. Basically, you need to "extend" this functionality to scan any .jar's in the .jar, and so on. So, the launcher may be like so:
    public void main (String[] args)
    JarClassLoader jcl = new JarClassLoader(this.getClass().getClassLoader());
    jcl.launchApp(this);
    the jcl.launchApp would be a method that tells the classloader to use THIS class, its "root" dir, to locate the .jar file to open and scan for more jar files. Each time it comes across a .jar file in the .jar, it opens it and scans it as well.
    Keep in mind, when the URLClassLoader contains one or more .jar files, when a class it loaded references a class that is not found in the that classloaders parent loader classpath, it then starts looking in the .jar files in the URL[] path specified to it. It looks at all classes in the .jar files, but it does NOT look in nested .jar files in the .jar file. Basically, you could take the source code of the URLClassLoader, and add the ability to open up any .jar files nested in each .jar file as needed to resolve a class. I would imagine this could take some time at runtime, probably a second or two on slower machines for a single typecast. However, that would be dependent on the number of nested .jar files, the number of .jar's in the classpath of the classloader. I think I may undertake this task when I have time. I gotta finish my plugin engine first, then I may undertake this classloader. I am pretty sure that is the proper approach to take. However, because it is not part of the JVM .jar execution mechanism, a special bit of coding will be required, such as what I posted above. The main class in the .jar file will have to create this classloader and tell it to "execute" the program, passing to it another main class, specifying the location of the .jar file the main class is in, etc. Hell, you could almost bypass the java -jar app.jar and just do a java Classname where by the class and the .jar are bundled together, although this wouldn't be as nice. The reason this might be better is that then you wouldn't have to do any special coding in the .jar main class. I suppose though, by providing the "launcher" class and having a developer just extend it as part of their main class could also work.
    }

Maybe you are looking for

  • How to create a picture presentation with text ?

    I need to create a 2 minutes video of a Bus rental company, the video has to be created with pictures text and voiceover, the text will be the subtitle of the voiceover so that the video can also be used without audio... I would like to make it in FC

  • How to include third party .jar files into my jar file

    Hi, how do i include a open source third party jar file into an executable jar? i have a class called BlogBox.java and it uses org.apache.commons.net.ftp.FTPClient, i want to create an executable file, where do i put the .jar file containing FTPClien

  • Best way to install Leopard on external HD

    My iMac came with Leopard. I upgraded to Lion, 3 months ago. I found out that Appleworks doesn't work anymore... very displeased because I always used Mac Strong points (for the past 30 years), which is drawing, like McDraw, McDraw 11, Clarisworks, A

  • Disadvantage of Smartform and Advantages of SAP script

    Good day Gurus.,, Can anyone please tell me the Advantages of SAP Script over Smartforms and Disadvantages of Smartforms over SAP script. Also is there any special scenario where only sapscript could be used but not Smartforms. Please enlighten me...

  • VSOM Not Showing Disk Usage for Media Servers

    I wanted to try and figure out why my Operations Manager is not synchronizing the disk usage of the media servers storage partition. Currently I am running a fully virtualized environment on a Cisco UCS-B series chassis with version VSM 7.5.1 (Redhat