2

I want to open New MS Word Document to open on button click in java can you suggest me the code , I am doing this by following code but i think its to open the existing document not to create the new document

class OpenWordFile {

    public static void main(String args[]) {

        try {
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd.exe /C start Employee.doc");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Exception occured" + ex);
        }

    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
ADESH RAJPUT
  • 131
  • 1
  • 5
  • 10

4 Answers4

1

You can not do this using Java alone, at least if you need to generate DOC Files you need a 3rd tool library Aspose for example. Take a look at this thread, otherwise you can open existing files using the runtime.

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

only the comment with out any words

Runtime run = Runtime.getRuntime();
String lcOSName = System.getProperty("os.name").toLowerCase();
boolean MAC_OS_X = lcOSName.startsWith("mac os x");
if (MAC_OS_X) {
    run.exec("open " + file);
} else {
    //run.exec("cmd.exe /c start " + file); //win NT, win2000
    run.exec("rundll32 url.dll, FileProtocolHandler " + path);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

In the recent release (Java 6.0), Java provides Desktop class. The purpose of the class is to open the application in your system that is associated with the given file. So, if you invoke open() method with a Word document (.doc) then it automatically invokes MS Word as that is the application associated with .doc files.

I have developed a small Swing program (though you can develop it as a console application) to take document number from user and invoke document into MSWord. The assumption is; documents are stored with filename consisting of <document number>>.doc.

Given below is the Java program that you can compile and run it as-it-is. Make sure you change DIR variable to the folder where .doc files are stored.

Here is the code to open Word Doc in Java... its an extract from net....

import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class WordDocument extends JFrame {
    private JButton btnOpen;
    private JLabel jLabel1;
    private JTextField txtDocNumber;

    private static String DIR  ="c:\\worddocuments\\";   // folder where word documents are present.

    public WordDocument() {
       super("Open Word Document");
       initComponents();
    }

    private void initComponents() {
        jLabel1 = new JLabel();
        txtDocNumber = new JTextField();
        btnOpen = new JButton();

        Container c = getContentPane();
        c.setLayout(new java.awt.FlowLayout());
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Enter Document Number  : ");
        c.add(jLabel1);

        txtDocNumber.setColumns(5);
        c.add(txtDocNumber);

        btnOpen.setText("Open Document");
        btnOpen.addActionListener(new ActionListener() {      // anonymous inner class 
            public void actionPerformed(ActionEvent evt) {
                  Desktop desktop = Desktop.getDesktop();  
              try {
                File f = new File( DIR + txtDocNumber.getText()  +  ".doc");
                 desktop.open(f);  // opens application (MSWord) associated with .doc file
              }
              catch(Exception ex) {
                // WordDocument.this is to refer to outer class's instance from inner class
                JOptionPane.showMessageDialog(WordDocument.this,ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
              }
            }
        });

        c.add(btnOpen);

    } // initCompnents()

    public static void main(String args[]) {
          WordDocument wd = new WordDocument();
          wd.setSize(300,100);
          wd.setVisible(true);
    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

Maybe using java.awt.Desktop can help?

    File f = new File("<some temp path>\\file.docx");
    f.createNewFile();

    Desktop.getDesktop().open(f);

Creates a new empty document and opens it with the systsem specifik program for the extension. The strength of this solution is that it works for all OS... As long as the OS has a program to view the file that is.

Although I suspect that you are looking for semthing with abit more control over the creation of the file...

David
  • 375
  • 4
  • 11
  • 2
    Did you try that? I did not, but I suppose Word won't open that document as it is just empty. I might be wrong. – home Sep 21 '12 at 11:50
  • Good comment! Actually it depends on the software that handles the file... Word actually accepts empty files as new files... Some fileformats on the otherhand will crash(PDF for one). – David Sep 21 '12 at 11:54