0

I have a program that allows a user to press a button, and then it runs a command line statement that the user is able to see the output of.

I now want to know if it's possible to stop the command before it finishes. I know of the command "ctrl+c" when running command prompt normally, however, I'm not sure how you would implement this in a java program.

runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent r) { JFrame runFrame = new JFrame("Running process..."); runFrame.setSize(500, 400); runFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); runFrame.setLayout(null); JButton somethingButton = new JButton("Stop"); JButton closeButton = new JButton("Close"); somethingButton.setBounds(65, 290, 105, 25); closeButton.setBounds(335, 290, 105, 25); JTextArea run = new JTextArea(); JScrollPane runSP = new JScrollPane(run); runSP.setBounds(65, 50, 375, 200); run.setLineWrap(true); run.setWrapStyleWord(true); run.setEditable(false); runFrame.add(runSP); runFrame.add(somethingButton); runFrame.add(closeButton); runFrame.setVisible(true); //Start of creating of command line stuff Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec("ping riot.de"); } catch (IOException e1) { System.err.print("Error: " + e1); e1.printStackTrace(); } try { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } run.setText(sb.toString()); } catch (IOException e) { System.err.println("Error: " + e); } closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent c) { runFrame.dispose(); } }); somethingButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent c) { //CODE TO STOP HERE } }); } }); 
9
  • 1
    System.exit(0); doesn't work for you? Commented Sep 20, 2016 at 10:23
  • @Stultuske I don't want to close the entire program. This is a small snippet. Commented Sep 20, 2016 at 10:25
  • so ... you want to 'stop' the application, without 'stopping' the application? you mean interrupt a thread? Commented Sep 20, 2016 at 10:27
  • @Stultuske So I'm running a command line statement within Java. I want to stop the command line statement without ending the Java application Commented Sep 20, 2016 at 10:28
  • You mean like Process::destroyForcibly ? Commented Sep 20, 2016 at 10:28

1 Answer 1

3

Try using the mentioned code to destry the process process.destroy();

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, I'll have a look
I'm getting this error: Local variable process defined in an enclosing scope must be final or effectively final
I believe the issue is bcz of the runFrame object which is declared locally in runButton.addActionListener() but is referred in the inner class for closeButton.addActionListener() and compiler wants that to be declared as final
Sorry, I'm still quite unaware of the stack traces. Is this it? Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: Local variable process defined in an enclosing scope must be final or effectively final at MainFrame$4$2.actionPerformed(MainFrame.java:177) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
that's correct @Plumel, I already posted the cause of issue in my previous comment.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.