1

I am writing to a .txt file in my java program and once you're done typing you hit enter. Then I want to open said file you were writing in, here is how I am trying to do this.

    PrintWriter writer ;
    try {
        writer = new PrintWriter(file.getPath(), "UTF-8");
        writer.println(decodedMessage);
        writer.close();
        try {
            pr = runtime.exec(file.getAbsolutePath());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

So to clear some variables up, the decodedMessage is the string that stores the stuff you were typing, it then writes it to a file thats path is predefined. runtime is a RunTime object and pr is a Process object. I want the file that was just written to to be opened up . But when I run this code I get the following error

    java.io.IOException: Cannot run program "c:\users\owner\this.txt": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.encdec.commandline.CommandLineRead.decodeFile(CommandLineRead.java:108)
at com.encdec.commandline.CommandLineRead.executeEncodingDirectory(CommandLineRead.java:44)
at com.encdec.listeners.ButtonCommand.actionPerformed(ButtonCommand.java:40)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
     Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 46 more

It is very long and intimidating and I just can't seem to find anyone online with the same issue. Any help is extremely appreciated!

2
  • 1
    You try to execute your file but is it executable? Commented Jun 11, 2016 at 14:21
  • I guess it is not as it is a text file so what do you want to do with your text file exactly? Commented Jun 11, 2016 at 14:22

3 Answers 3

2

Could you please replace following line:

pr = runtime.exec(file.getAbsolutePath());

by this line:

pr = runtime.exec("notepad "+file.getAbsolutePath());

add tell me the result?

From the absolute path c:\users\owner\this.txt of file, it looks like that you are on Windows platform. But in case you want to open the file in platform independent manner, following lines may help you:

if(!GraphicsEnvironment.isHeadless()){
    java.awt.Desktop.getDesktop().open(file);
}else{
    System.out.println("No display is available.");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just finished my solution too and that's it.
1

I am not sure what you trying to exec calling a text file;

I may recommend reading the file with BufferedReader for example and output the text to JTextArea or if you don't want to use GUI output it to console as the official example shows:

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
  public static void main(String[] argv) throws Exception {

    BufferedReader in = new BufferedReader(new FileReader(file.getAbsolutePath()));
    String str;
    while ((str = in.readLine()) != null) {
      System.out.println(str);//outputs to console
    }
    in.close();
  }
}

p.s. anyways remember using exec means you know for sure the application (with which you want to open text file) is really installed;

Comments

1

If you want it to open in the system text editor (or whatever program is configured to open files with that file extension), do:

java.awt.Desktop.getDesktop().open(file);

https://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html#open-java.io.File-

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.