-2

I have the below code where I made a simple GUI. I would like Button2 to navigate to class 'Project2', which should start another piece of code. Just to note, in its current state, 'Project2' has no GUI, though I intend to add one soon. Anyway, this 'code jump' which I used by adding: String[] args = {}; Project2.main(args); is not working, as the IDE says 'IOException must be caught or thrown'. I know how this works, though I am not sure how to implement it in the program.

Thanks in advance!

LMB
  • 3
  • 1
  • 3
  • Why would you do it like that though? Edit: If you have another "section" of your program that is more or less independent, just do `Something s = new Something(maybeArgsHere); s.show(); ` or `s.main()` unless you really need to get an instance of this program statically, I guess. Also, to catch errors, surround your call with a `try {} catch ()` block. – Wep0n Aug 07 '17 at 08:26
  • You should *never* perform a long operation in an event handler. And it sounds like a `main` from another program is a long operation. It should be done in a worker thread, and I'd say that using another class's `main` is a big code smell that says you are in the wrong direction. – RealSkeptic Aug 07 '17 at 08:31
  • @RealSkeptic +1 - It does sound like it. I would suggest adding an answer to this explaining the virtues of threads for performance. Though, seeing as this user has 1 rep, I'm assuming they're not only new to the site but also new to the programming language they're asking about, so it might be the case that coming at them with threads for java could be a little much for a beginner there. What I think is probably the case, is that `Project2.main()` isn't even that long of an operation, as that project probably isn't that big anyway. I think your answer would make for some "good practices". – Wep0n Aug 07 '17 at 11:35
  • @Wep0n this is Swing. You can't **not** use threads. Even a `Thread.sleep(100)` is a long operation that should not be used in an event handler, because it will freeze the entire GUI. Let alone any input reads etc. which are likely in beginners' program. Anyway, one doesn't write an answer for "good practice". Only answer a question with something that directly answers the question. – RealSkeptic Aug 07 '17 at 11:42

2 Answers2

1

You can try to use dynamic class loading for your program. Below you can find lambda, which calls main method from com.stackoverflow.ExternalCaller class.

If you do not like to use lambda, you can create a simple anonymous class.

button.addActionListener(s -> {
    try {
         Class<?> externalCaller = Class.forName("com.stackoverflow.ExternalCaller");
         Method main = externalCaller.getDeclaredMethod("main", new Class[]{String[].class});
         main.invoke(null, new Object[]{new String[0]});
    } catch (ClassNotFoundException e) {
         e.printStackTrace();
    } catch (NoSuchMethodException e) {
         e.printStackTrace();
    } catch (IllegalAccessException e) {
         e.printStackTrace();
    } catch (InvocationTargetException e) {
         e.printStackTrace();
    }
});

ExternalCaller class in its turn looks something like that:

package com.stackoverflow;

public class ExternalCaller {
    public static void main(String args[]) {
        System.out.println("Hello World");
    }
}

In result once you click on the button you will get Hello World output in console. If you would like to work with external jars etc. please look on Process class. Quick example:

Process proc = Runtime.getRuntime().exec("java -jar External.jar");

Or even more on fork/exec. You can read From Runtime.exec() to ProcessBuilder for more details.

Hope this will help. Good luck.

0

In most of the IDE's, when you right-click on the Button2 in the Design(GUI) pane, you can travel through:

Events -> Actions -> actionPerformed().

And write this code in the selected method to switch classes:

this.setVisible(false); //turns off the visibility of the current class
outputClass out = new outputClass(); //creating the object of the class you want to redirect to
out.setVisible(true);//turns on the visibility of the class you want to redirect to
Brijesh Shah
  • 172
  • 8