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);
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
  • Try with this `C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe` and `D:\\02 Tu Jaane Na.mp3` and take a look at this http://stackoverflow.com/questions/7171840/launch-vlc-player-through-java – RanRag Feb 11 '12 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);`. – Martin McNulty Feb 11 '12 at 10:20
  • @ RanRag : but why did not it worked with (/) ?? – shubhendu mahajan Feb 11 '12 at 10:22
  • @ Martin McNulty : no it works fine but does not play the song.it just display the vlc player's screen – shubhendu mahajan Feb 11 '12 at 10:23
  • @shubhendumahajan: AFAIK, it should work with either \\ or /, the difference is that RanRag added the file extension `.mp3` to the filename – esaj Feb 11 '12 at 10:59
  • please post output of `dir "D:/02 Tu Jaane Na"` – Oleg Mikheev Feb 11 '12 at 11:17

2 Answers2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 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 – Oleg Mikheev Feb 11 '12 at 11:20
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 :)

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95