0

Following code opens status very fine in notepad:

import java.util.*; class test { public static void main(String args[]) { try{ ProcessBuilder pb=new ProcessBuilder("notepad","F:/status"); pb.start(); }catch(Exception e) { System.out.println(e); } } } 

Following code does'not play the song:

import java.util.*; class test { public static void main(String args[]) { try{ ProcessBuilder pb=new ProcessBuilder("C:/Program Files (x86)/VideoLAN/VLC/vlc","D:/02 Tu Jaane Na"); pb.start(); }catch(Exception e) { System.out.println(e); } } } 
6
  • Try with this C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe and D:\\02 Tu Jaane Na.mp3 and take a look at this stackoverflow.com/questions/7171840/… Commented Feb 11, 2012 at 10:11
  • What happens? Does it print out an exception? Does it launch VLC at all? Also, generally 'e.printStackTrace();` gives you more information than System.out.println(e);. Commented Feb 11, 2012 at 10:20
  • @ RanRag : but why did not it worked with (/) ?? Commented Feb 11, 2012 at 10:22
  • @ Martin McNulty : no it works fine but does not play the song.it just display the vlc player's screen Commented Feb 11, 2012 at 10:23
  • @shubhendumahajan: AFAIK, it should work with either \\ or /, the difference is that RanRag added the file extension .mp3 to the filename Commented Feb 11, 2012 at 10:59

2 Answers 2

2

I think that the problem is that you're ignoring the fact that the files you're trying to open have filename extensions.

Windows Explorer doesn't display file extensions by default - that is probably why you are not aware of their existence.

The reason why notepad worked in your first example is that notepad automatically adds .txt extension to its filename parameter in case you didn't provide one yourself. So in reality the file that is being open is not status but status.txt.

VLC doesn't have this "advanced" functionality because there's no specific filename extension it is designed to work with.

So you will need to look up the dir command output and add the full file name as a parameter.

If that was the real issue - you might want to modify your Windows Explorer settings for it to display file extensions:

enter image description here

or, which is better, switch to a more programmer-friendly OS :)

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

Comments

2

For 1.6+ code, use Desktop.open(File) instead.


Of course, the sensible thing to do immediately before calling that is to check File.exists().

OTOH, Desktop.open(File) throws a slew of handy exceptions, including:

  • NullPointerException - if file is null
  • IllegalArgumentException - if the specified file doesn't exist
  • UnsupportedOperationException - if the current platform does not support the Desktop.Action.OPEN action
  • IOException - if the specified file has no associated application or the associated application fails to be launched

Properly handled, the exception would indicate the immediate problem.


As an aside, the Desktop class is designed to be cross-platform, and will handle any file type for which an association is defined. In that sense it is a lot more useful for something like this, than trying to use a Process.

1 Comment

Desktop.open will open an associated application. In Windows association is done via file extension only, and the file he(?) tries to open is called D:/02 Tu Jaane Na so Desktop.open will fail

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.