0

I wanted to open a file in notepad(using JAVA) by...

proc.exec("notepad "+curDir+"\auto_saved.txt");

where...

 String curDir = System.getProperty("user.home");
 curDir=curDir+"/txt-files";                
 Runtime proc = Runtime.getRuntime();

this works properly in WIN7 but says 'home..........\auto_saved.txt' PATH not found. in LINUX. but if I save a file to (curDir+"\auto_saved.txt");..it creates a file and saves it in LINUX.

So, how to open a file in /home/user/some-folder/...??

3
  • 1
    There is no notepad app on linux. Commented Aug 18, 2011 at 2:50
  • The string literal "\auto_saved.txt" is not valid Java and will cause a compile error. It'd have to be "\\auto_saved.txt". Are you sure that's your real code? Commented Aug 18, 2011 at 3:17
  • no error..n working fine in WIN7...for ubuntu its "/auto_saved.txt". Commented Aug 18, 2011 at 3:22

2 Answers 2

2

You're expecting the \ in "\auto_saved.txt" to be a directory separator, but in Linux it isn't, it's just an ordinary character that can be part of a filename. Use File.pathSeparator (a static field in java.io.File) instead; it'll be / on Linux/Unix and \ on Windows.

Sign up to request clarification or add additional context in comments.

2 Comments

\ is generally an escape character in Linux/Unix. It could be used in a filename but it would have to be double escaped - touch \\file\\ - creates a file named \file\.
It's also an escape character in Java, and \a isn't a valid escape sequence, so "\auto_saved.txt" actually won't compile.
0
  1. There is no notepad editor in Linux. Many linux distributions have a text editor called "nano", in /usr/bin/nano; Ubuntu/Debian puts a text editor in /etc/alternatives/editor ; you could also also check env{EDITOR}, which sometimes contains a user set pathname to a text editor, and override your choice of editor if present to allow for user customization.
  2. You might want to use the full path to the executable when you call exec, in case it is not on the path.

3 Comments

I tried this..String pathWIN7=curDir+"/auto_saved.txt"; shows.."C:/users/abhishek/txt-files/auto_saved.txt String pathLINUX="txt-files/auto_saved.txt";shows.."txtfiles/auto_saved.txt" //means no need to give "home/user/.." path //but i saved file to "home/user/txt-files/auto_saved.txt" proc.exec("notepad "+pathWIN7);//if os.name is windows 7 proc.exec("notepad "+pathLINUX);//if os.name is Ubuntu this is working.........bt..could not understand why ..
there is a notepad as part of WINE project for Ubuntu. However, it is not part of a standard Ubuntu install.
You might check to see if the process returns to fast. There is a program that runs briefly, when I enter "notepad a.txt" to a shell prompt, "The program 'notepad' can be found in the following packages: * wine1.2 * wine1.0 Try: sudo apt-get install <selected package>" I wonder if this will also appear if you try to call up notepad from java within Ubuntu.

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.