Results 1 to 11 of 11

Thread: Help with this

  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default Help with this

    I am using th Runtime.getRuntime.exec function to execute external processes on Windows. I was wondering is there an equivalent function to do the same thing on Linux and if so, how is it done?
    Help is greatly appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Of course. The same one, but using the proper command, of course.

  3. #3
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Ok, so how would I go about using it?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The same way you do on Windows.

    Why don't you try telling us exactly what it is you want to do.

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Hi,Cactus,if you want to execute 'ls -Als' command in linux examine the following code:

    Java Code:
    import java.lang.* ;
    import java.io.*;
    public class Java
    {
          public static void main(String[] args)throws Exception
          {
                String cmd = "ls -Als" ;
                Runtime run = Runtime.getRuntime() ;
                Process pr = run.exec(cmd) ;
                pr.waitFor() ;
                BufferedReader buf = new BufferedReader( new InputStreamReader(pr.getInputStream() ) ) ;
                String line ;
                while((  line = buf.readLine() ) != null )
                {
                     System.out.println(line) ;
                }
    
         }
    
    }

  6. #6
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Hi, this is the code I am using in Windows:


    public class OpenMovie
    { //class

    public static void main(String args[])
    { //main



    try //try statement
    { //try
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "Videos\\Movie.wmv"); //open the file movie.wmv

    } //try

    catch (Exception e) //catch any exceptions here
    { //catch
    System.out.println("Error" + e ); //print the error
    } //catch

    } //main

    } //class


    I just want to do the same thing using Linux.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    If you have 1.6 checkout the Desktop class in the API docs and it's open method. With that you can implement a solution that works on all platforms (that support Desktop which most do) without having to use different commands on different systems.

  8. #8
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Thanks. Could you give the link to the docs for the Desktop class?

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Links...

    Desktop class & methods:
    Desktop (Java Platform SE 6)
    For future reference, Java 1.6 API specification:
    Java Platform SE 6

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Hi, I have looked at the docs, but I don't understand how it's done. So, could someone please post a code example?
    Help is greatly appreciated.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Dr... you have to learn to start to search for stuff on your own.... using an method is not that dificult. Here's some links to help you... please use them.

    How to Integrate with the Desktop Class (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Other Swing Features)

    You can search & find a lot of help in this link:

    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •