外部jar中的异常处理

时间:2014-02-17 13:06:19

标签: java swing exception-handling

我写了两个java桌面应用程序。其中一个(我称之为启动器)启动另一个(客户端)应用程序。 这两个应用程序都是在java 1.6.0_32 [oracle jvm,hot spot]下编译的。 Exception处理程序在客户端应用程序的主线程中指定。

在java 1.6.0_32下启动启动器时一切正常, 但是当它在1.7x_xx以后开始时,异常处理程序开始工作不正常。 它没有任何例外。

我已经编写了测试示例,列出了源代码

启动器应用来源:

Start.java:

import javax.swing.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

public class Start {

    private ClassLoader classLoader;
    private List<URL> urls = new ArrayList<URL>();

    private LauncherFrame launcherFrame = new LauncherFrame();

    private Start() {
        urls = generateUrls();
    }

    public static void main(String[] args) {
        new Start().initClassLoader(args);
    }

    public void initClassLoader(String[] args) {
        launcherFrame.initComponents();
        this.classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Start.class.getClassLoader());
        try {
            Class<?> mainClass = classLoader.loadClass("my.example.Runner");
            Object instance = mainClass.newInstance();
            Method runMethod = mainClass.getMethod("run", null);
            runMethod.invoke(instance);
        } catch (Exception e) {
            System.out.println("exception: ");
            e.printStackTrace();
        }
    }

    public List<URL> generateUrls() {
        File jarFile = new File("prog2.jar");
        List<URL> urls = new ArrayList<URL>();
        if (jarFile.exists()) {
            try {
                urls.add(jarFile.toURI().toURL());
            } catch (MalformedURLException e) {
            }
        }
        return urls;
    }

    class LauncherFrame extends JFrame {
        public void initComponents() {
            // if i comment next line, everything works fine in java 6 and 7
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        }
    }

}

客户端应用源(prog2.jar):

Runner.java:

package my.example;

public class Runner {

    public static void main(String args[]) {
        new Runner().run();
    }

    public void run() {

        ThreadGroup exceptionHandler = new ThreadGroup("Global Exception Handler") {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("UncaughtException method called");
                e.printStackTrace();
            }

        };

        Runnable runnable = new MainThread();
        Thread thread = new Thread(exceptionHandler, runnable);
        thread.start();
    }
}

MainThread.java:

package my.example;

import java.awt.*;

public class MainThread implements Runnable {

    public void run() {
        Toolkit.getDefaultToolkit().getSystemEventQueue().push(new MyQueue());
        MainFrame w = new MainFrame();
        w.open();
    }

    public class MyQueue extends EventQueue {
        protected void dispatchEvent(AWTEvent event) {
            super.dispatchEvent(event);
        }
    }
}

MainFrame.java:

package my.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;

public class MainFrame extends WindowAdapter {

    private JFrame frame;

    public MainFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(200, 200));
        JPanel panel = new JPanel();

        JButton button = new JButton("BREAK THIS");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                breakThis();
            }
        });

        panel.add(button);
        frame.setContentPane(panel);
    }

    public void breakThis() {
        throw new RuntimeException("EXCEPTION OF CLICK");
    }

    public void open() {
        this.frame.setVisible(true);
    }
}

0 个答案:

没有答案