-1

am developing a java application in which I am using swings to develop GUI screens. i am supposed to run some application files. which I did by connecting to command prompt by using Runtime.exec() method. if my application failes to execute properly then a GUI frame will come up asking weather to run that file again or to skip.

here my problem is when I say run that file again the control should return to the point where the frame is called using ui.setvisible(true);

if not the swing frame what can i use to make my code work

public static boolean runFormat(String format,String buildNumber) throws Exception
{
try{
        ProcessExecutor process = new ProcessExecutor();
         process.executeCommand(format+"\\Scripts"+File.separator+"Step1.bat"+""+"02_00"+" "+format);
        process.waitForCompletion();
        File file = new File(format+File.separator+"Results1.log");
        BufferedReader read = new BufferedReader (new FileReader(file));
        String line;
        while((line=read.readLine())!=null)
        {
            if(line.contains("Successful exit."))
    {
                return true;
    }
        }
        return false;
}
catch(Exception e)
    {
        System.out.println("EXCEPTION OCCURED..................");
        System.out.println("JTag has failed for "+format);
        e.printStackTrace();
}
    return true;
}
    void run(Set<String> formats)
    {
        try
        {
        for(String ar : formats)
        {
            boolean b =runFormat(ar,"001");
            if(b==false)
            {
                ExampleUi ui = new ExampleUi();
                ui.setVisible(true);
            }
        }
        }
        catch(Exception e)
        {

        }
    }

Thanks in advance

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2572003
  • 743
  • 3
  • 8
  • 16
  • What does your description have to do with your question title exactly? Also can you provide some code so we can better understand what you are trying to achieve? – Paul Samsotha Jan 13 '14 at 03:18
  • 1
    "what can i use to make my code work" - More/better code. Hard for us to help without a [MCVE](http://stackoverflow.com/help/mcve) – Java Devil Jan 13 '14 at 03:31
  • from above code i will call the GUI screen and when i give click on button in screen i should return to run method – user2572003 Jan 13 '14 at 03:38
  • You should edit your post to include the code. It will be formated and syntax highlighted and then delete those comments – Java Devil Jan 13 '14 at 03:40

1 Answers1

0

The short answer is no.

The long answer would involve using a SwingWorker and making the decisions about what to do within it's done method

Take a look at Worker Threads and SwingWorker for more details...

public class ProcessWorker extends SwingWorker<Boolean, Void> {
    public Boolean doInBackground() throws Exception {
        ProcessBuilder pb = new ProcessBuilder(...);
        Process p = pb.start();
        // Read the input stream in separate thread...
        return p.waitFor() == 0;
    }

    public void done() {
        try {
            boolean okay = get();
            if (!okay) {
                // Re-run....?
            }
        } catch (Exception exp) {
            // Show error message, maybe in a JOptionPane
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hi am in the above code will get invoke the doInBackground method ???????????? am sorry i have never used swingWorker class – user2572003 Jan 13 '14 at 06:15
  • Something like `new ProcessWorker().execute()`, take a look at the linked tutorial – MadProgrammer Jan 13 '14 at 06:19
  • my code is working properly when i debug but when i run its getting terminated with out executing anything – user2572003 Jan 13 '14 at 06:55
  • Do you have some kind of frame visible on the screen? – MadProgrammer Jan 13 '14 at 06:57
  • i dint use any frames just i used some conditions to print msg. i 1st wanted to practise – user2572003 Jan 13 '14 at 06:59
  • SwingWorker probably utilising a daemon thread, meaning that the jvm is free terminate almost immediately. Have a look at [this](http://stackoverflow.com/questions/15396694/swing-message-doesnt-get-displayed-until-after-runtime-getruntime-exec-fini/15398441#15398441) and [this](http://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490) examples... – MadProgrammer Jan 13 '14 at 07:02
  • public static void runInBackgroud(final String msg) { SwingWorker task = new SwingWorker ( ) { public Boolean doInBackground() { System.out.println(msg); return true; } public void done(){try {Boolean b= get(); if(b==true) { System.out.println("correct hai"); } else { System.out.println("in correct hai"); }} catch (Exception e) {} }};task.execute ( ); } } – user2572003 Jan 13 '14 at 07:02
  • No the frame is not visible :( – user2572003 Jan 13 '14 at 08:08
  • If you're just testing the SwingWorker, just create a simple frame that you can make visible while you test it – MadProgrammer Jan 13 '14 at 08:32
  • yes.... i did thats what is the problem when i debug its working fine but when i run its doing nothing........... – user2572003 Jan 13 '14 at 08:57