Can MS Word Macros be executed with Runtime Exec.

When I open the Run Dialog box in Windows and type in - WINWORD.EXE c:\test.rtf /n /mFilePrint /mFileExit
the file - C:\test.rtf is printed using the Macros FilePrint and the application closes.
Now when I try to do the same in java using an instance of the Runtime class and pass the exec method a String[] with the same contents , it cannot find the executable WinWord.exe .
So i used the cmd /c option and specified the file name to open the file.
Runtime.getRuntime().exec("cmd /c \""+newfile+"\""); // newFile = String rep of path to the file But when i use /m which is a MS Word command line switch - The program compiles successfully and does nothing.Is there any way of executing the Macro

have a look at this here article:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Similar Messages

  • Launch Acrobat Reader with Runtime.exec() under WINNT4, without absolut pat

    Hi,
    I would like to launch Acrobat Reader from a Swing application. This works fine with Runtime.exec() as long as I provide the full path to the AcroRd32.exe. The problem is that I cannot predict the location of this exe on the systems of the users. Athough is in the Windows PATH, since I can launch from the windows execute-dialog, the Runtime.exec() is not able to locate the file.
    How can I solve this problem?
    Thanks a lot!
    Horst

    hey BIJ, thank you so much man. start really does wonders!! i was stuck with invoking a fortran program for last couple of days and now i went through your postings and tried with start. it did work!!
    thanks again,
    bhairampally

  • Can we run a java application using Runtime.exec()?

    Can we run a java application using Runtime.exec()?
    If yes what should i use "java" or "javaw", and which way?
    r.exec("java","xyz.class");

    The best way to run the java application would be to dynamiically load it within the same JVM. Look thru the class "ClassLoader" and the "Class", "Method" etc...clases.
    The problem with exec is that it starts another JVM and moreover you dont have the interface where you can throw the Output directly.(indirectly it can be done by openong InputStreams bala blah............). I found this convenient. I am attaching part of my code for easy refernce.
    HIH
    ClassLoader cl = null;
    Class c = null;
    Class cArr[] ;
    Method md = null;
    Object mArr[];
    cl = ClassLoader.getSystemClassLoader();
    try{
         c = cl.loadClass(progName.substring(0,progName.indexOf(".class")) );
    } catch(ClassNotFoundException e) {
    System.out.println(e);
         cArr = new Class[1] ;
         try{
         cArr[0] = Class.forName("java.lang.Object");
         } catch(ClassNotFoundException e) {
         System.out.println(e);
         mArr = new Object[1];
         try{
         md = c.getMethod("processPkt", cArr);
         } catch(NoSuchMethodException e) {
         System.out.println(e);
         } catch(SecurityException e) {
         System.out.println(e);
    try {            
    processedPkt = md.invoke( null, mArr) ;
    } catch(IllegalAccessException e) {
              System.out.println(e);
    } catch(IllegalArgumentException e) {
              System.out.println(e);
    }catch(InvocationTargetException e) {
              System.out.println(e);
    }catch(NullPointerException e) {
              System.out.println(e);
    }catch(ExceptionInInitializerError e) {
              System.out.println(e);
    }

  • Problem with Runtime.exec()

    Hi,
    I'm having a problem with Runtime.exec() . I have a batch file with debug option to FTP the desktop file to mainframe. When I executed this in Windows it's shows me the responce from the server (like ' Transfer Completed' ) for my FTP commands. But when I launch this through java it's only shows the errors not the normal responces from the server. I need this responses to confirm the proper transfer of file.
    My Java Code:
    String strCommand = "cmd.exe /c " + outdirectory + batchfilename;
    boolean bWait = true;
    Runtime r = Runtime.getRuntime();
    Process pr = r.exec(strCommand);
    BufferedInputStream bis =new BufferedInputStream(pr.getInputStream ());
    int c=0;
    /** Outlet for IO for the process **/
    while (c!=-1) {
    c=bis.read();
    /**Now wait for the process to get finished **/
    if(bWait == true){
    pr.waitFor();
    pr.destroy();
    My Batch file is :
    FTP -n -d -s:C:\IT2.cmd > C:\tIT2.log
    EXIT
    Thanks
    Sathiesh

    Ahh, you are redirecting the standard out from the ftp command to a file, therefore no output is sendt back to your java code,
    you have to redirect the stderr stream, not the stdout stream in your batch file;
    FTP -n -d -s:C:\IT2.cmd 2> C:\tIT2.log(notice the "2>" instead of ">")
    Now your batch file should direct the error stream to the log, and the standard output back to the java program.
    (I'm used to UNIX streams, not NT, but the "2>" redirection should work on NT also I guess)

  • Killing Internet Browser opened with Runtime.exec

    I am running Win NT 4.0 and I am successfully opening my HTML help using the following code:
    String command = new String("cmd /K ");
    command = command.concat("G:\editor\help\index.html");
    Process p = Runtime.getRuntime().exec(command);
    However, if I run p.destroy(), the browser window does not close. This means that the help remains open after the program exits and also that multiple help windows can be opened.
    Does anyone know how to kill a browser opened with Runtime.exec()?
    Thanks for any help you can offer,
    Christopher Collins
    Marineering Limited

    Thanks jmschrei,
    I tried System.exit(0) and Runtime.getRuntime().exit(0) and neither succeeded in closing the internet browser process. I suspect that process.destroy() and runtime.exit(0) both are killing the instance of the command interpreter spawned by runtime.exec("cmd /K URL") and not actually the browser opened by that command. Therefore, I think that the process refers to the "cmd" and not the browser, which probably opens completely independently of my Java program, thus can't be closed by it.
    Unforetunately, I don't know another way to open a browser.
    If anyone has any additional suggestions, I would appreciate it. Many people in the forums seem to have asked this one, and there are no solutions I could find.
    Christopher

  • Problem with Runtime.exec(gcc -c...

    when I execute
    P=Runtime.exec("cmd.exe /C gcc -c file.c");
    I do not have any problem but ,I do not obtain the corresponding file .o
    Somebody can help me, please?

    Are you checking the command is running. Unless you read the output of the process you will not see any errors.
    try
         public static void main(String[] args) throws IOException, InterruptedException {
              Process p1 = Runtime.getRuntime().exec("cmd.exe /C echo hello");
              System.out.println("Exit code = " + p1.waitFor());
              // produces an error.
              Process p2 = Runtime.getRuntime().exec("cmd.exe /C dontecho hello");
              System.out.println("Exit code = " + p2.waitFor());
         }

  • Problem with runtime.exec().It hangs Up

    Hi all,
    I am having a problem with the runtime.exec method.I am trying to execute linux commands using this method.For most of the commands it works fine.But when i tried to change the user with the su command in linux my program hung up.So please help me to get around this.any help would be highly appreciable..
    I am pasting the code..
    <code>
    Process p=null;
    int ch=0;
    try
    System.out.println("Before executing command");
    String unlock_command="sh changeuser.sh";
    p = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c","su tony"});
    InputStreamReader myIStreamReader = new InputStreamReader(p.getInputStream());
    while ((ch = myIStreamReader.read()) != -1)
    System.out.print((char)ch);
    p.waitFor();
    int p_exitvalue = p.exitValue();
    System.out.println("After executing the command and the exit value = "+p_exitvalue);
    p.destroy();
    catch (IOException anIOException)
    System.out.println(anIOException);
    catch(Exception e)
    e.printStackTrace();
    </code>
    Thanks
    HowRYou

    Hi sabre,
    What you have pointed out is right.But if i change the user as root then it will not ask for a password.Isn't it.Anyway thank you for giving your suggestions.Can yoiu help me more.Waiting for all of your help.I will try to swoitch between different users othere than root by giving the password.So just help me.

  • Executing a runtime.exec statement from an ejb

    Hi all,
    relativly new to j2ee. I need to use the runtime.exec command to uncompress a set of files from an ejb.
    So far, ive tried that and even the most basic commands like
    /usr/bin/ll *
    and i don't get any response back other than: exit value = 2
    when executing:
    Runtime runtime = Runtime.getRuntime();
    // Process proc = runtime.exec("/usr/bin/uncompress "+this.localFileStore+"*.Z");
    Process proc = runtime.exec("/usr/bin/ll * ");
    // put a BufferedReader on the ls output
    InputStream inputstream = proc.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
    // read the ls output
    String line;
    while ((line = bufferedreader.readLine()) != null) {
    System.out.println(line);
    // check for ls failure
    try {
    if (proc.waitFor() != 0) {
    System.err.println("exit value = " + proc.exitValue());
    catch (InterruptedException e) {
    System.err.println(e);
    No output from the buffered reader. Any one have any ideas as to why i cannot execute any runtime commands? This is also related to the thread i created where i'm looking for a java library for uncompressing a unix compressed file (unix compress = *.z and not zip or gz files). I think my only two options are to either use the runtime to uncompress these files or.. use some java library to uncompress these files. Let me know if anyone can offer any hints/helpfull comments/libs
    Thanks a bunch

    I might be wrong on this one, but I believe using Runtime.exec() would generally be frowned upon when invoked from an EJB. The reason? EJB implies a remote call. Can you guarantee that a given native process or service will be available on all possible remote nodes that the EJB might be deployed on? Maybe. Can it actually be achieved? Yes. Is it in the spirit of EJB? Probably not.
    - Saish

  • Launch Command Line Scanner with Runtime.exec()

    Hi im trying to develop all the process of a common Antivirus ,
    I have a command line anti-virus. But when i try to scan a single file mi code doesnt work.
    I made the following code:
    Runtime runtime = Runtime.getRuntime();
    commands = new String[] {"cmd.exe", "/c","C:\\KAVCLineScanner\\kavecscan.exe","C:\\anfed\\a.txt"};
    Process p =runtime.exec(commands);
    BufferedReader b =new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String line=null;
    while( (line=b.readLine())!=null){
    System.out.println(line);
    I think the problem is my parameter where i send the file to scan "C:\\anfed\\a.txt", because if i execute this instruction from MSDOS, the process doesnt have problems.
    I also made a .bat file with the instructions but i have the same problem :(
    Thanks for any advise

    I think java naming is from programmer's point of view, so the output stream of the process would be the inputstream to the programmer, hence I am trying to write to my outputstream, which is actually the process' inputstream.

  • Problems with Runtime.exec() and certain Unix processes

    Certain Unix processes don't behave correctly when run from Java using Runtime.exec(). This can be seen by running /bin/sh and trying to interact with it interactively. The issue appears to be that /bin/sh (and many other Unix tools) are checking the file handles on the spawned process to see if they are associated with a TTY.
    Is there any way to associate a process spawned by Runtime.exec() with a terminal or alternatively, is there a JNI library available that would setup the process correctly and still provide access to the input and output streams?
    Our objective is to have the flexibility of expect in being able to run and interact with spawned processes. A bug was opened at one point but closed back in 1997 as being a fault in the spawned process, not Java.
    Bug ID: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4058689

    #include <stdio.h>
    void doit() {
    int c;
            while((c=getc(stdin)) != EOF) {
                    printf("%c",c);
    int main() {
            freopen("/dev/tty", "r", stdin);
            doit();
    }This program reopens its standard input against /dev/tty and catenates it to stdout. And voila, it takes its standard input from the terminal, even though it was started with stdin against /dev/null..
    $ gcc b.c -o b
    $./b < /dev/null
    buon giorno
    buon giorno

  • Problems with runtime.exec()

    Hi All,
    I am having a problem using runtime.exec()
    in servlet to call a java file from the server.The server is a linux one and i am using a tomcat server-4.1.24 the server contains
    some java files which i need to invoke with the help of the servlet program so i try to use exec() in servlet to invoke a particular java file in the server.The program is compiling well but no information
    is displayed on the browser.Where in the servlet program i try to print the details of the particular java program that is in the server.For ur
    reference i will post the code :-
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;
    public class ExeServlet extends HttpServlet
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    try
    String path = getServletContext().getRealPath("/var/jakarta/webapps/examples/WEB-INF/classes/ServletExample.java");
    /*Where it is the path of the java file that resides in the server machine(linux)Which i want to invoke and print its details on the browser*/
    Runtime runtime=Runtime.getRuntime();
    Process proc=runtime.exec(path );
    BufferedReader br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();
    pw.println("<b>");
    String line=null;
    while((line=br.readLine())!=null)
    { pw.println(line); //Displaying the details of the ServletExample.java file in the browser
    pw.println("</b>");
    catch (Exception e)
    pw.println("Listener *not* started!");
    What is the problem here?I was so frustrated with this.Pls. do provide an immediate reply if there is any code regarding this is working pls. do provide it.It is Urgent.I will be waiting for ur reply.
    Thanx,
    m.ananthu

    Hi Leo,
    Thank u for the reply i still have the problem with exec() in servlets.Has i said earlier in my previous mail by using exec() i trying to execute the output of a c-program for eg,Hello.run which provides an output of "HelloWorld" which is a sample eg.The Hello.run is the directory /var/jakarta/webapps/examples/WEB-INF/classes which is also the directory where my servlet program is.Where i am using a tomcat server-4.1.24 on the linux machine 7.3.Which is the server ofcourse.
    Here by using a servlet program i try to exceute the Hello.run c-program and display the output in the browser.This is what i need to do.
    I also used a sample core java class put the exec() there in a method and i try to call the method in the servlet.Which is also not working.
    Here is that example :-
    The core java program:-
    public class UsingExec
    public String ExeDisplay()
         try
    System.out.println("ExeDisplay");
         Runtime rt=Runtime.getRuntime();
         Process p=rt.exec("/var/jakarta/webapps/examples/WEB-INF/classes/Hello.run");
    p.waitFor();
    return "true";
         catch(Exception e)
    return "false";
    public static void main(String args[])
    UsingExec ue=new UsingExec();
    ue.ExeDisplay();
    When i run this program only ExeDisplay at the top is displayed.
    Here is the Servlet program:-
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;
    public class ExeServlet extends HttpServlet
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    try
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();
    UsingExec ue=new UsingExec();
    String line=ue.ExeDisplay();
    pw.println(line);
    catch (Exception e)
    When i display this program on the browser only "false" is displayed from the UsingExec.java & nothing is displayed.
    Thanx,
    m.ananthu

  • Any problem with Runtime.exec()

    I am required to launch a local exe from an applet. At present I am using AppletContext.showDocument().The problem with this is that in netscape a box is popped up forcing the user to save that exe file. The requirwment is to run it directly without any message being asked.
    A workaround is to use Runtime.exec(), it works, however I have heard that it is not platform independent.
    (1) Is there a better solution than Runtime.exec() ?
    (2) If not could you give me some info on Runtime.exec() and the problems associated with it ?
    (3) Is there a way to use AppletContext.showDocument() without the message being asked ?
    Shikari.

    First of all, Java is platform independent language, and has many security restriction in it's applets.
    so from an applet you can't excute a program using "Runtime.getRuntime().exec()".
    coz. there is security restriction on that.
    for the "AppletContext.showDocument()" this methid is for displaying the specified document on the client browser, so it can't excute a client program.
    if u need to use Runtime.getRuntime().exec() you have to get the client agreement for that.
    see the applet security restriction for more info.

  • Intermittent Error with Runtime.exec()

    I have a Windows Service for an Adaptive Server Anywhere database server that is built upon install of our application.
    One of the new things we're trying to do with our app
    requires us to shutdown this service using Java (1.4.2_06) code. This
    shutdown is done by using the runtime.exec("dbsvc -x
    ASA_SERVICE") command. Sometimes this works fine, but
    others it fails. I got the error output from the java
    command which is: Overlapped I/O operation is in progress.
    Error stopping service "ASA_SERVICE". When I try and
    shutdown the service via the Windows Service dialog I have
    no problems anytime. I see no errors in the ASA log
    file. Can someone tell me what might be
    causing the error sometimes when running this using the Java
    runtime.exec(...) command?
    Thanks,
    Cory Musselman

    Try running the command a couple of dozen times from the command line, not from Runtime.exec() but just from a plain old command line. That should give you a better idea of whether Runtime.exec() is part of the problem or not.

  • A problem with "Runtime.exec"

    Hello!
    I'm just trying to call a Fortran executable in a Java program.
    I'm a beginner in Java language and I believed Runtime.exec was the solution. My code :
    try {
    Process process = null;
    Runtime runtime = Runtime.getRuntime();
    process = runtime.exec("<name>.exe");
    catch (IOException eio) {
    System.out.println("Error -- " + eio.toString());
    When I run the project, there's no error, but the executable is not called.
    Can anybody help me?

    process = runtime.exec("Make sure// you have the right// path to.exe");

  • Error in opening a file with name in chinese characters with Runtime.exec

    The issue at hand is when I try to open a file with file name containing chinese characters in a localized environment in Windows through the following java code:
    Runtime.exec("rundll32 SHELL32.DLL,ShellExec_RunDLL {File_With_FileName_containing_Chinese_character}");
    the following error is thrown by windows.
    Cannot open file "C:\??.txt".
    with the exception
    java.io.IOException: CreateProcess: [Ljava.lang.String;@f5da06 error=2
            at java.lang.Win32Process.create(Native Method)
            at java.lang.Win32Process.<init>(Win32Process.java:66)
            at java.lang.Runtime.execInternal(Native Method)
            at java.lang.Runtime.exec(Runtime.java:566)
            at java.lang.Runtime.exec(Runtime.java:428)
            at java.lang.Runtime.exec(Runtime.java:364)
            at java.lang.Runtime.exec(Runtime.java:326)
            at Launcher.main(Launcher.java:26)
    When I try to use the same command (shown below) from the Windows Run command, the file opens sucessfully
    rundll32 SHELL32.DLL,ShellExec_RunDLL {File_With_FileName_containing_Chinese_character}
    Please suggest.
    Thanks in advance

    This may be a file association problem.  To solve that:
    In Windows 7:
    1. Right-click the file,
    2. Select "Open With" 
    select [Your Program Here]
    3. Check always use this program. 
    In Windows XP:
    1. Locate the file as you have described above
    2. Right click the file
    3. Select Open with from the pop-up menu
    4. Click Choose default program…
    5. Select Excel in the Recommended Programs box
    6. Check Always use the selected program to open this kind of file
    7. Click on OK.
    1.  Make Excel not available from Office 2007 listed under Programs and Features in Control Panel.
    2. Take a registry backup using the steps given here
    3. In the registry editor window expand HKEY_CLASSES_ROOT and navigate to .xls right and delete it.
    4. Exit Registry Editor
    5. Undo Step 1 to make Excel available.
    6.  Restart the computer and verify it the issue is fixed.
    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Cheers, Shane Devenshire

Maybe you are looking for