• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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.
0
Naeemg
Asked:
Naeemg
  • 3
  • 3
  • 3
  • +2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now