Results 1 to 15 of 15
  1. #1
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default Create an executeable file for java

    Helo every one.i've created an a simple project in java.After that I've builded them and in a appeared directory named DIST i see a main file named Java_Store.jar and one subdirectory LIB within this files
    jcalendar-1.3.3.jar
    mysql-connector-java-5.1.6-bin.jar
    . Then i start to run a Java_Store.jar - all okey. BUt then i replace DIST in another plave the error Java_Store.jar are appeare again and again..How i can create a one executable jar file for running in any place that i want.
    Thanks for all

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    in your build process, is it possible to have a repackage target, that would unjar the third party jar files into your built classes folder and then your jar file target would create a single jar of your code and the repackaged third party pieces ?

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need to move/distribute the entire DIST directory. That is why all that stuff is found there. Unpacking the third-party jars and repackaging them into your jar is laible to cause licensing problems.

  4. #4
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    Sorry..The problem was with my MYSQL CONNECTOR cause iam from Russia and if i move a DIST in a place that contain Russian word's in a path-an error occure..With english words in path no problem.
    From Russia with Love!

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That should not cause any problem (assuming your classes are also in a jarfile) as the classpath in the mainfest is relative to the location of the jarfile, so anything in the path before that point makes no difference and everything else is from that point down, so any characters of any language in the path before that point (including the name of DIST itself, which you can also change) should not cause any problems what-so-ever.

    What you can't do (without manually editing the manifest) is change the names of the sub directories or jars in them. And, if you do change those names, and change the manifest to reflect that, then you will need to stick to ASCII range characters.

  6. #6
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    but it is true ...Then i put DIST in directory named rusia words-the problem is appear..With English-no problem..MAy be cause of this problem is not a My sql..May be something another reazon...But it is not good

  7. #7
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    Watch this file please
    PHP Code:
    webfile.ru/4054529

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by cowboy View Post
    Watch this file please
    PHP Code:
    webfile.ru/4054529
    And what is this suppossed to mean?

  9. #9
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    it is a link for my DIST..If you want - you can watch this ..May be some error in manifest

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by cowboy View Post
    but it is true ...Then i put DIST in directory named rusia words-the problem is appear..With English-no problem..MAy be cause of this problem is not a My sql..May be something another reazon...But it is not good
    Like I said, as long as you are moving the entire directory (you can even rename the directory itself) everythign should be okay. If this is not the case, search the Java bug database and if you don't find anything there, report a bug to the Java bug database.

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by cowboy View Post
    it is a link for my DIST..If you want - you can watch this ..May be some error in manifest
    So, is that suppossed to be
    WebFile - скачать бесплатно Java_Store.rar
    or something else?

    Edit: Well, seeing as how the site has automatically resolved and labeled it, I guess so.

  12. #12
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    yes..Get the link and download my DIST please..i can not set a direct link because i create less then 20 post in my presence at this forum..Use this link

    webfile.ru/4054529

  13. #13
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Oh yea, that's right. I didn't think about the licensing problems with redistributing other 3rd party .jar files.

    Ok, how about if we invented a custom launcher, such as using our own custom class loader, which we configure to be the jar files that we discover to exist in the same folder as the original .jar file that was launched with "java -jar".

    In this example, it assumes you have this in a package "jarlauncher" in your codebase. And that you have set the "Main-Class" attribute in your manifest file to point to this launcher class (instead of the usual class you would execute). and then to also set a (new) custom manifest file attribute "Launcher-Main-Class", which is the full package and class to the application you used to launch from java -jar.

    This launcher determines the file system folder that the .jar file is sitting in on your system, and then attempts to find all of the "*.jar" files in that same folder (what ever it is, and where ever or how ever you invoke java -jar from). That is to say, it no longer requires you to be in the same folder of the .jar file.

    And because the third party jar files are in the same folder as this .jar file, there is no licensing problems, or possible funny "DIST" folder needed.

    Java Code:
    /**
     *Created on Nov 3, 2009
     */
    package jarlauncher;
    
    import java.io.File;
    import java.io.FileFilter;
    import java.lang.reflect.Method;
    import java.net.JarURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.security.CodeSource;
    import java.security.ProtectionDomain;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.jar.Attributes;
    import java.util.jar.JarFile;
    import java.util.jar.Manifest;
    
    
    /**
     * A launcher to automatically create a classpath to contain all .jar files that are in the same folder
     * as where ever this .jar file is sitting on the file system.
     * 
     * <p>You need to set a new "Launcher-Main-Class" property in your MANIFEST.MF file in your executable .jar file
     * (instead of your application like it used to be).
     * and set "Main-Class" to be this class.
     * For example,
     * <pre>
     * Main-Class: jarlauncher/SimpleJarLauncher
     * Launcher-Main-Class: your.package.and.Class
     *
     * @author thein
     */
    public class SimpleJarLauncher {
    
      /**
       * The custom attribute name to look for in our manifest file.
       */
      private static final String LAUNCHER_MAIN_CLASS="Launcher-Main-Class";
    
      /**
       * Inspects the .jar file that contains this class, finds the manifest file. 
       * reads the Launcher-Main-Class property.
       * Determines the file system path to this jar file.
       * Attempts to find other *.jar files in the same folder
       * Creates a URL class loader of the set of jar files found.
       * Used this custom class loader to load and invoke main() in the configured Launcher-Main-Class thing.
       * @param args what ever was passed to main();
       */
      private SimpleJarLauncher(String[] args) throws Exception {
    
        URL jarUrl = new URL("jar:" + getJarURL().toExternalForm() + "!/");
        JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile();
        
        // read the Launcher-Main-Class value from this jar's MANIFEST.MF file.
        Manifest jarManifest = jarFile.getManifest();
        Attributes manifestAttributes = jarManifest.getMainAttributes();
        String mainClassName = manifestAttributes.getValue(LAUNCHER_MAIN_CLASS);
        
        if (mainClassName == null) {
          throw new IllegalArgumentException("please set attribute " + LAUNCHER_MAIN_CLASS + " in your MANIFEST.MF file inside the .jar file to the package and class you want to launch.");
        }
        
        List<URL> urls = new ArrayList<URL>();
        
        String path = jarUrl.getPath();
        //urls.add(jarUrl);
        
        // convert the jar:// url into a file:// url.
        path = path.substring(0, path.lastIndexOf("/"));
        if (path.endsWith(".jar!")) {
          path = path.substring(0, path.lastIndexOf("/"));
        }
        if (path.startsWith("file:")) {
          path = path.substring("file:".length());
        }
        if (!path.endsWith("/")) {
          path += "/";
        }
        
        // look for all *.jar files in this same folder as the main jar file.
        File jarfolder = new File(path);
        File[] entries = jarfolder.listFiles(new FileFilter() {
          public boolean accept(File pathname) {
            return (!pathname.isDirectory() && pathname.getName().endsWith(".jar"));
          }
        });
        if (entries != null) {
          for (int i = 0; i < entries.length; i++) {
            URL url = entries[i].toURI().toURL();
            urls.add(url);
          }
        }
        
        // now build a URL class loader with the bunch of .jar files we found (including our jar file i
        URLClassLoader urlClassLoader = new URLClassLoader((URL[]) urls.toArray(new URL[0]));
    
        // and make this new class loader be used for the rest of this Java application.
        Thread.currentThread().setContextClassLoader(urlClassLoader);
        
        // we expect the MAIN_CLASS set above to contain a public static void main(String[] args) method
        // we use our new class loader to load the main class 
        // and then invoke it's main()
        Class<?> mainClass = null;
        try {
          mainClass = urlClassLoader.loadClass(mainClassName);
        }
        catch (Exception ex) {
          throw new IllegalArgumentException("class not found in your jar file " + mainClassName);
        }
        
        // ok, class loaded, now find the main method
        Method mainMethod = null;
        try {
          mainMethod = mainClass.getMethod("main", String[].class);
        }
        catch (Exception ex) {
          throw new IllegalArgumentException("class to launch (" + mainClassName + ") does not have a public static void main(String[]) method");
        }
        
        // and invoke main()
        mainMethod.invoke(null, (Object) args);
      }
      
      /**
       * Constructs a URL expression for the location to this current class, where it lives in the jar file.
       * 
       * @return
       * @throws MalformedURLException
       */
      protected URL getJarURL() throws MalformedURLException {
        Class<?> cls = this.getClass();
        ProtectionDomain domain = cls.getProtectionDomain();
        CodeSource codeSource = domain.getCodeSource();
        URL sourceLocation = codeSource.getLocation();
        return new URL(sourceLocation.toString());
      }
      
      public static void main(String [] args) throws Exception {
        new SimpleJarLauncher(args);
      }
    }
    Since the manifest file is used to set the name of the real application to be launched, and since it passes through all the command line parameters that were given to the .jar file, it should be transparent and should work without needing to modify it, we just have to stuff it into our code base and have the thing that builds you the .jar file (ant task?) to make sure the manifest file contains our custom Launcher-Main-Class,

    I tested this with a couple of my current projects, using a manifest like this:
    Java Code:
    Main-Class: jarlauncher.SimpleJarLauncher
    
    #replace this with what your main class and package are.
    Launcher-Main-Class: org.mypackage.MClass
    Note that if there used to be a "Class-Path" entry in your manifest file and if this contained references to the DIST or jar files you wanted on your classpath, you should likely remove it now, this launcher builds the classpath for us now.

    Hope that helps.
    Last edited by travishein; 11-03-2009 at 08:56 PM.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by travishein View Post
    OOk, how about if we invented a custom launcher, such as using our own custom class loader, which we configure to be the jar files that we discover to exist in the same folder as the original .jar file that was launched with "java -jar".
    Complete and total overkill, and probably quite fragile. Simply let the IDE compile the stuff and take the entire "DIST" folder (and you can rename that folder to whatever you want) and distribute it (as long as those third party jars allow redistribution, and if not notify the user to download them, or have your program do it with web start, and place them there). No muss, no fuss.

    I don't know what the entire deal here is. There is no problem with and no need for any "fancy" or complicated, or overly sophisticated solution, as, once again, there is no problem here.

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by cowboy View Post
    yes..Get the link and download my DIST please..i can not set a direct link because i create less then 20 post in my presence at this forum..Use this link

    webfile.ru/4054529
    The manifest file looks good, simply make sure that you leave the "DIST" directory exactly as is (except that you can rename it) and it should work. If it does not, and the only thing changed is the location (and name) of the DIST directory, and not its contents, thn go to Bug Database and file a bug report.

    This does, of course, assume that you are attempting to run the jar through the command (or with a double-click when done through an OS GUI) and not through Netbeans.

Similar Threads

  1. Replies: 10
    Last Post: 11-01-2009, 10:42 PM
  2. Replies: 5
    Last Post: 08-25-2009, 09:43 AM
  3. How to create .exe file using .class file
    By pradnya in forum New To Java
    Replies: 8
    Last Post: 04-18-2009, 01:50 AM
  4. How to create exe file in java
    By radix in forum New To Java
    Replies: 8
    Last Post: 11-06-2008, 05:17 PM
  5. Replies: 20
    Last Post: 12-17-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •