0

i know that to open an application say notepad the following command is enough

ProcessBuilder proc=new ProcessBuilder("notepad.exe"); and executing

proc.start(); notepad open`s up an untitled file.

But i read that using processbuilder we can open a particular file in notepad with following command ProcessBuilder proc=new ProcessBuilder("notepad.exe","sample");

proc.start(); executing this

if a sample.txt notepad file doesnt exist in the system, notepad would open up and shows a message window asking that "sample.txt doesnt exist, do you want to create?". Well that condition is fine with me.

But before executing proc.start(); if i create a sample.txt file and save it in my desktop. And then upon executing proc.start(); shouldnt the file named sample open up in notepad and show me its content. Rather its still asking me to "do you want to create sample.txt". why is that so even if i had already created the same file before execution ?

techloris_109
  • 547
  • 5
  • 13
  • 2
    Are you sure you're in the right working directory? And did you create the file in a separate process, or in the same? – Gerard van Helden May 30 '15 at 15:50
  • 1
    You pass "sample" in the code sample, but it's "sample.txt" (with **.txt**) in the error message. Could this be the problem? I mean check if the file you are creating manually and programmatically has the same name. And yes, make sure you're in the right working directory or pass the full path to the process. – vempo May 30 '15 at 15:59
  • Consider not even using Process or ProcessBuilder but rather using Desktop as per [this question](http://stackoverflow.com/questions/3487149/how-to-open-the-notepad-file-in-java). – Hovercraft Full Of Eels May 30 '15 at 16:23
  • Well directory was the problem.. and thanks. when i saved sample.txt file in my program`s class folder it worked. I wonder how to open the sample.txt file which is somewhere else and not inside my program class folder. How should i code – techloris_109 Jun 01 '15 at 15:08

0 Answers0