-3

I want to make my program to execute this installer (SnakeInstaller.exe). I am using the Runtime class but I get an unhandled IOException and when I run the code i get an error:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException".

I have tried using the throw IOException but it just gave me another error.

public Board() {
    //This sets the color for the score label and where it is going it be.
    setLayout(null);
    scoreLabel.setForeground(Color.white);
    add(scoreLabel);
    scoreLabel.setBounds(625, 665, 100, 50);

    //This sets the color for the info label and where it is going it be.
    infoLabel.setForeground(Color.white);
    add(infoLabel);
    infoLabel.setBounds(5, 665, 500, 50);

    difficultyLabel.setForeground(Color.white);
    add(difficultyLabel);
    difficultyLabel.setBounds(600,650,100,50);

    //This starts the key listener and sets the background color of the JPanel.
    addKeyListener(new TAdapter());
    setBackground(Color.black);
    setFocusable(true);

    //This sets the size of the board.
    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
    //Goes the the loadImages method and difficulty method.
    loadImages();
    difficulty();

    Runtime.getRuntime().exec("X:/Snake/SnakeInstaller.exe");
}
Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
Weston
  • 1
  • The code failed to compile, so why are you trying to run it? Fix the compilation error first. Since [`exec()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String-) throws the **checked exception** `IOException`, you must handle it or allow it to cascade. If you don't know what a *checked exception* is, now is the time to research it, e.g. by searching the web or reading your Java guide. – Andreas Mar 01 '18 at 16:31
  • Thank you and I was running it because I use eclipse instead of the cmd to compile and run. – Weston Mar 01 '18 at 16:36
  • In Eclipse, the code is compiled as soon as you save the file. It often shows errors even *before* you save it. The compilation error is fully visible before you run, and Eclipse will even ask for confirmation to try to run the code when a compilation error is present, so that is a very bad excuse. You explicitly accepted to run code with errors, so use of IDE is not an answer to why you're trying to run code that failed to compile. – Andreas Mar 01 '18 at 16:39
  • Im fairly new to coding and i was testing to see if the rest of my code was still running with that part not working so im sorry that i ran it with a compilation error. – Weston Mar 01 '18 at 18:05

1 Answers1

0

You tried to run a program although you still had compilation errors. That's what the exception is telling you.

The compilation error is that Runtime.getRuntime().exec(...) can throw an IOException, and that you have to do something about it (as it is a so-called "checked exception"):

  • Either declare it in the throws clause of your Board() constructor, as in public Board() throws IOException {. Of course, then the same applies to places where you use the constructor new Board(). Then that method also has to get a throws IOException clause, and so on, up to the top level, often being the main() method.
  • Or catch the exception, and decide wisely what to do in the catch(IOException ex) part. I'd guess that your program can't continue without having the SnakeInstaller run. So a good approach might be to throw new RuntimeException(ex); there (this one being "unchecked", meaning that you can throw it without declaring it). That makes sure the program won't silently continue, producing nonsense, and the original exception's information (e.g. stacktrace) are still there for you to inspect them. Another approach would be to do ex.printStacktrace(); and System.exit(1); Or, if you know a way how to continue even without the SnakeInstaller, just show a warning message to the user.

Of course, there's a whole lot of other options, so I'd recommend to read about Java's exception system (to learn the theory) and the many stackoverflow questions on that topic (to learn about good usage patterns).


To explain a bit more on the throw new RuntimeException(ex) approach:

public Board() {
    //This sets the color for the score label and where it is going it be.
    setLayout(null);
    scoreLabel.setForeground(Color.white);
    add(scoreLabel);
    scoreLabel.setBounds(625, 665, 100, 50);

    //This sets the color for the info label and where it is going it be.
    infoLabel.setForeground(Color.white);
    add(infoLabel);
    infoLabel.setBounds(5, 665, 500, 50);

    difficultyLabel.setForeground(Color.white);
    add(difficultyLabel);
    difficultyLabel.setBounds(600,650,100,50);

    //This starts the key listener and sets the background color of the JPanel.
    addKeyListener(new TAdapter());
    setBackground(Color.black);
    setFocusable(true);

    //This sets the size of the board.
    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
    //Goes the the loadImages method and difficulty method.
    loadImages();
    difficulty();
    try {
        Runtime.getRuntime().exec("X:/Snake/SnakeInstaller.exe");
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
  • Where would i use the throw new RuntimeException(ex); at, I have tried looking it up but didnt find a way that explained it and i tried multiple places and none of them worked – Weston Mar 01 '18 at 21:30
  • When i tried that i got a very large error message – Weston Mar 02 '18 at 14:08
  • @Weston if you post the complete error message, we can help you understand it. – Ralf Kleberhoff Mar 02 '18 at 15:29
  • Sorry i havent been on my computer all weekend but here is the link @Ralf Kleberhoff [link](https://pastebin.com/U01aBgTK) – Weston Mar 05 '18 at 14:12
  • In the error message, there are three parts: "java.lang.RuntimeException: ..." with the line number 104 of the `throw new RuntimeException(ex);`statement. Then there's "Caused by: java.io.IOException: Cannot run...", giving the file name. That's what exec() throws, and you'll find your line 101 in the stack trace. This, in turn is "Caused by: java.io.IOException [without useful info]" coming from some internal code. So it tells you that for some IO-related reason, it couldn't execute the given command. – Ralf Kleberhoff Mar 05 '18 at 15:51
  • If you try to execute that command from a command line, does it start? Or, if you exchange the Java line with the path to e.g. notepad.exe, does that start from Java? – Ralf Kleberhoff Mar 05 '18 at 15:53
  • Could it be that i have another method throwing an ioexception? @Ralf Kleberhoff – Weston Mar 05 '18 at 17:15
  • Also changing it to where notepad is worked – Weston Mar 05 '18 at 17:27
  • The file i am trying to execute is an executable jar file so it should work as a .exe correct or do i need to use .jar? – Weston Mar 05 '18 at 18:11
  • @Weston Try on cmd.exe until you have a command line that works on its own, then integrate that into your Java program. And if it's a .jar then trying to start a non-existent .exe surely won't work. – Ralf Kleberhoff Mar 05 '18 at 18:40
  • I got it to work by making a .bat file for it instead of the .jar that i was using. – Weston Mar 07 '18 at 14:19