Results 1 to 10 of 10

Thread: Run other commands or applications..

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    17

    Run other commands or applications..

    Hi there..

    I've a question on Java; how can I run a command from the Java code, for example, I want when click on a JButton to send command to the system such as "dir" or "copy" or run applications by something like "start winword"..

    plz help me it's so important for me..

    I've another question: is there a component I can use to show an HTML page inside it on a frame, how it's used. I use JBuilder 5.

    Thanx2all.
    Excelsoft IT

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    As for your first question as far as i remember to run an application System.exec() was used but im not too sure though.

    To display an html page you can use a javax.swing.JEditorPane which is able to handle frames, forms, hyperlinks, and parts of css Level 1. When you create your JEditorPane you can either pass in the actual html in the constructor with the mime type or you can just set the initial page with a Url passsed in as an arguement.

    As i said before, JEditorPane is a able to handle hyperlinks. This is achieved with the use of HyperlinkListener interface or more specifically javax.swing.event.HyperlinkListener which contains this one method that you must implement. public void hyperlinkUpdate(HyperlinkEvent e)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    17

    I'll see..

    thanx 4 ur reply, I'll check that then tell u.
    thanx again.
    Excelsoft IT

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I should add some things.

    For the copy and dir commands, you'd best do that in code via the java.io.File class.
    Executing applications is done with System.exec (or maybe it was Runtime.getRuntime().exec).
    However, you cannot "execute" data files, like word documents. I wrote a small JNI class to do that, but I still need Linux and MacOS implementations.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    17
    thanx CornBee 4 ur reply, but I'm beginner in Java may u please write an example of how these commands are to be written.
    Excelsoft IT

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, there's no copy command. Strange...

    To list the files of a directory, use File.list or File.listFiles.
    Code:
    import java.io.File;
    
    public class Dir {
      public static void main(String[] args) {
        File cwd = new File(".").getAbsolutePath();
        System.out.println("Index of " + cwd);
        System.out.println();
        String[] files = cwd.list();
        for(int i = 0; i < files.length; ++i) {
          System.out.println(files[i]);
        }
      }
    }
    Usage:
    java Dir

    You can execute any executable using Runtime.exec().
    Code:
    public class Start {
      public static void main(String[] args) throws Exception {
        if(args.length < 1) {
          System.out.println("You must specify a file.");
          return;
        }
        Runtime.getRuntime().exec(args[0]);
      }
    }
    Usage:
    java Start <executable name>
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    17
    thanx again I'll try this and tell you..
    Excelsoft IT

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    17

    Thumbs up

    Thanx , it worked!!
    Excelsoft IT

  9. #9
    New Member
    Join Date
    Feb 2004
    Posts
    6
    umm.. is it possible to code where java is able to click and parse information(such as password and username) to the third party program that was run...

    example:
    java runs my msn messenger and press the sign in button
    it then fills the username and password and press Ok button for me...

    I hope something like this is possible. If it is, pls guide me. Thank you.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can look at the java.awt.Robot class, but it's some unpleasant stuff to do.

    Which is why good applications should always provide a command line interface.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width