Changeset 624


Ignore:
Timestamp:
12/13/13 16:06:50 (11 years ago)
Author:
jts21
Message:

Add runJar action that loads and runs an executable jar file in the memory space of Expeditee.
Doesn't work very well (closing the launched application will call System.exit(), which results in Expeditee also closing).
The idea was to see if any generic java program could be loaded inside of an Expeditee widget. As it turns out, they can be loaded, but not safely and probably not inside of a widget unless there's some way to capture a window creation in java.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Misc.java

    r623 r624  
    44import java.awt.Desktop;
    55import java.awt.Image;
    6 import java.awt.Point;
    76import java.awt.Polygon;
    87import java.awt.image.BufferedImage;
    98import java.awt.image.VolatileImage;
    10 import java.io.BufferedReader;
    119import java.io.File;
    1210import java.io.FileNotFoundException;
    1311import java.io.IOException;
    14 import java.io.StringReader;
    1512import java.lang.reflect.Method;
     13import java.net.URL;
     14import java.net.URLClassLoader;
    1615import java.util.ArrayList;
    1716import java.util.Collection;
    1817import java.util.LinkedList;
    1918import java.util.List;
     19import java.util.jar.Attributes;
     20import java.util.jar.JarFile;
    2021
    2122import javax.imageio.ImageIO;
     
    3637import org.expeditee.gui.TimeKeeper;
    3738import org.expeditee.importer.FrameDNDTransferHandler;
    38 import org.expeditee.io.ExaReader;
    39 import org.expeditee.io.ExaWriter;
    40 import org.expeditee.io.FrameWriter;
    4139import org.expeditee.io.WebParser;
    4240import org.expeditee.io.flowlayout.XGroupItem;
     
    4442import org.expeditee.items.ItemUtils;
    4543import org.expeditee.items.Line;
    46 import org.expeditee.items.Picture;
    4744import org.expeditee.items.Text;
    48 import org.expeditee.items.XRayable;
    4945import org.expeditee.items.widgets.InteractiveWidget;
    5046import org.expeditee.items.widgets.JfxBrowser;
     
    13751371                FrameUtils.CreateDefaultProfile(UserSettings.UserName, homeFrame);
    13761372        }
     1373       
     1374        /**
     1375         * Loads and runs an executable jar file in a new Thread
     1376         * @param jar path to the jar file to run
     1377         */
     1378        public static void runJar(String jar) throws Exception {
     1379                File jf = new File(jar);
     1380                if(!jf.exists()) {
     1381                        System.err.println("jar '" + jar + "' could not be found");
     1382                        return;
     1383                }
     1384                JarFile jarFile = new JarFile(jf);
     1385               
     1386                String mainClassName = (String) jarFile.getManifest().getMainAttributes().get(new Attributes.Name("Main-Class"));
     1387                if(mainClassName == null) {
     1388                System.err.println("jar '" + jar + "' does not have a Main-Class entry");
     1389                jarFile.close();
     1390                        return;
     1391        }
     1392                jarFile.close();
     1393                System.out.println("Main-Class = " + mainClassName);
     1394               
     1395                ClassLoader classLoader = ClassLoader.getSystemClassLoader();
     1396
     1397                Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
     1398                addURL.setAccessible(true);
     1399                addURL.invoke(classLoader, jf.toURI().toURL());
     1400
     1401                final Class<?> jarClass = classLoader.loadClass(mainClassName);
     1402               
     1403                final Method main = jarClass.getDeclaredMethod("main", String[].class);
     1404               
     1405                new Thread(new Runnable() {
     1406                        public void run() {
     1407                                try {
     1408                        main.invoke(jarClass, new Object[] {new String[0]});
     1409                } catch (Exception e) {
     1410                        System.out.println("Failed to start jar");
     1411                        e.printStackTrace();
     1412                }
     1413                        }
     1414                }).start();
     1415        }
    13771416}
Note: See TracChangeset for help on using the changeset viewer.