Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Runtime.getRuntime().exec(new String[]{ "rundll32",
- "url.dll,FileProtocolHandler", fileName });
- Desktop desktop = Desktop.getDesktop();
- desktop.open( aFile );
- desktop.imaginaryAction( aFile );
- package your.pack.name;
- import java.io.File;
- public class Desktop{
- // hide the constructor.
- Desktop(){}
- // Created the appropriate instance
- public static Desktop getDesktop(){
- String os = System.getProperty("os.name").toLowerCase();
- Desktop desktop = new Desktop();
- // This uf/elseif/else code is used only once: here
- if ( os.indexOf("windows") != -1 || os.indexOf("nt") != -1){
- desktop = new WindowsDesktop();
- } else if ( os.equals("windows 95") || os.equals("windows 98") ){
- desktop = new Windows9xDesktop();
- } else if ( os.indexOf("mac") != -1 ) {
- desktop = new OSXDesktop();
- } else if ( os.indexOf("linux") != -1 && isGnome() ) {
- desktop = new GnomeDesktop();
- } else if ( os.indexOf("linux") != -1 && isKde() ) {
- desktop = new KdeDesktop();
- } else {
- throw new UnsupportedOperationException(String.format("The platform %s is not supported ",os) );
- }
- return desktop;
- }
- // default implementation :(
- public void open( File file ){
- throw new UnsupportedOperationException();
- }
- // default implementation :(
- public void imaginaryAction( File file ){
- throw new UnsupportedOperationException();
- }
- }
- // One subclass per platform below:
- // Each one knows how to handle its own platform
- class GnomeDesktop extends Desktop{
- public void open( File file ){
- // Runtime.getRuntime().exec: execute gnome-open <file>
- }
- public void imaginaryAction( File file ){
- // Runtime.getRuntime().exec:gnome-something-else <file>
- }
- }
- class KdeDesktop extends Desktop{
- public void open( File file ){
- // Runtime.getRuntime().exec: kfmclient exec <file>
- }
- public void imaginaryAction( File file ){
- // Runtime.getRuntime().exec: kfm-imaginary.sh <file>
- }
- }
- class OSXDesktop extends Desktop{
- public void open( File file ){
- // Runtime.getRuntime().exec: open <file>
- }
- public void imaginaryAction( File file ){
- // Runtime.getRuntime().exec: wow!! <file>
- }
- }
- class WindowsDesktop extends Desktop{
- public void open( File file ){
- // Runtime.getRuntime().exec: cmd /c start <file>
- }
- public void imaginaryAction( File file ){
- // Runtime.getRuntime().exec: ipconfig /relese /c/d/e
- }
- }
- class Windows9xDesktop extends Desktop{
- public void open( File file ){
- //Runtime.getRuntime().exec: command.com /C start <file>
- }
- public void imaginaryAction( File file){
- //Runtime.getRuntime().exec: command.com /C otherCommandHere <file>
- }
- }
- public static boolean isWindows() {
- String os = System.getProperty("os.name").toLowerCase();
- return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
- }
- public static boolean isMac() {
- String os = System.getProperty("os.name").toLowerCase();
- return os.indexOf("mac") != -1;
- }
- public static boolean isLinux() {
- String os = System.getProperty("os.name").toLowerCase();
- return os.indexOf("linux") != -1;
- }
- public static boolean isWindows9X() {
- String os = System.getProperty("os.name").toLowerCase();
- return os.equals("windows 95") || os.equals("windows 98");
- }
- if (isLinux())
- {
- cmds.add(String.format("gnome-open %s", fileName));
- String subCmd = (exec) ? "exec" : "openURL";
- cmds.add(String.format("kfmclient "+subCmd+" %s", fileName));
- }
- else if (isMac())
- {
- cmds.add(String.format("open %s", fileName));
- }
- else if (isWindows() && isWindows9X())
- {
- cmds.add(String.format("command.com /C start %s", fileName));
- }
- else if (isWindows())
- {
- cmds.add(String.format("cmd /c start %s", fileName));
- }
- final Program p = Program.findProgram(fileExtension);
- p.execute(file.getAbsolutePath());
- Program.launch("http://google.com/");
- Display.syncExec(new Runnable() {
- public void run() {
- Program.launch("http://google.com/");
- }
- });
Add Comment
Please, Sign In to add comment