how to execute any command using RUN in windows by Java program

HI EE>
Is it possible to execute any command using RUN in windows from within my java program.
LVL 5
NaeemgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
use Runtime.exec() method
0
NaeemgAuthor Commented:
suppose i want to load help contents using RUN.
In RUN command i write HH  "C:\Application\ApplicationContent.chm::/CopyRight_Information.htm"
in result help contents opens.
how can i call RUN to load this command/
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

objectsCommented:
try:

"start C:\Application\ApplicationContent.chm::/CopyRight_Information.htm"
0
NaeemgAuthor Commented:
i tried but not opening. also i've to include HH before path.
0
sciuriwareCommented:
"start HH C:\Application\ApplicationContent.chm::/CopyRight_Information.htm"
<*>
0
Mayank SAssociate Director - Product EngineeringCommented:
Which version of Windows are you on?
0
WebstormCommented:
Hi Naeemg,

Try :

RunProcess rp=new RunProcess(

        Runtime.exec("HH  \"C:\\Application\\ApplicationContent.chm::/CopyRight_Information.htm\""),

        // or:
        // Runtime.exec("cmd.exe /C start HH  \"C:\\Application\\ApplicationContent.chm::/CopyRight_Information.htm\""),

        System.in,false,
        System.out,false,
        System.err,false);



the RunProcess class is :

import java.io.*;

public class RunProcess
{
    private Process p=null;
    private static final int BUFFER_SIZE=8192;
    private Thread th_in=null,th_out=null,th_err=null;
    private int ret_code=-1;

    public RunProcess(
        final Process p,
        final InputStream p_in,   final boolean in_close,
        final OutputStream p_out, final boolean out_close,
        final OutputStream p_err, final boolean err_close)
    {
        this.p=p;

        // Process. OUT
        (th_out=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            InputStream inp=p.getInputStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        if (p_out!=null) p_out.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        // Process. ERR
        (th_err=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            InputStream inp=p.getErrorStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        if (p_err!=null) p_err.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        if (p_in!=null)
        // Process. IN
        (th_in=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            OutputStream outp=p.getOutputStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=p_in.read(b);
                        outp.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        (new Thread(){
            public void run()
            {
                try{
                    ret_code=p.waitFor();
                }catch(Exception ex){}
                if (th_in!=null) th_in.interrupt();
                try{if (in_close&&(p_in!=null)) p_in.close();}catch(Exception ex){}
                try{if (out_close&&(p_out!=null)) p_out.close();}catch(Exception ex){}
                try{if (err_close&&(p_err!=null)) p_err.close();}catch(Exception ex){}
                if (th_out!=null) th_out.interrupt();
                if (th_err!=null) th_err.interrupt();
            }
        }).start();
    }

    public int getReturnCode()
    {
        return ret_code;
    }

    public int waitFor() throws Exception
    {
        return p.waitFor();
    }

    public void destroy()
    {
        p.destroy();
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Wow.... too much of code ;-)
0
WebstormCommented:
>>Wow.... too much of code ;-)
But working code :-)
and with the RunProcess class i created, you can easily redirect output & input to file or other output/input stream.
0
NaeemgAuthor Commented:
thank u Webstorm.
0
WebstormCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.