Results 1 to 8 of 8

Thread: DOS Commands in Java?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Does anyone know if i could enable my console app to
    make use of dos commands? And if so what package would
    i have to import? Im pretty sure of what i remember in C++
    we had to use include <process.h>




  2. #2
    Guest
    java.lang.Runtime.getRuntime().exec(command);

    Where command is a string such as "notepad".

    This returns java.lang.Process if you need more functionality.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I found what you are talking about in one of my java books
    ("Java in a Nutshell") But once again Java's concepts seem
    to elude me. {{{laughing}}} It says " The Process class
    is abstract and cannot be instantiated" ok i understand that. But then they go on to say "Call one of the Runtime.exec() methods to start a process and return a corresponding Process object" Why must Java talk speak to
    me in Riddles???

  4. #4
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425
    Java in a Nutshell is more of a reference manual than a turorial. Try downloading the Java Trail from java.sun.com...

  5. #5
    Guest
    The exec method is overloaded which means it can take different signatures to call it. The signature I spoke of was exec(String). There is also exec(String[]) and others.

    Try it without returning a Process Object and that will probably be all you need.

    java.lang.Runtime.getRuntime().exec("notepad");
    should launch notepad.

    Process p = java.lang.Runtime.getRuntime().exec("notepad");
    would create a Process Object p that might be usefull for something else while still launching notepad. You may not need this extra utility (usefullness) of having p.

  6. #6
    Guest
    Yeah, I started here at vb-world when it was only VB, now they added other languages.

    You'd have to think about "What does 'cls' mean?".

    From a dos shell, the command means to clear the screen, but from a java application, you may be writing to a dos shell as output; the dos shell doesn't seem to work as a "slave" window to do whatever you say from java. You might want to use your own display window (try Frame or Dialog) in java.

    I'd think of the dos shell window as an area I can write output. Did the word "notepad" appear in the dos shell when you launched notepad from java? I don't think it would (but I didn't try it). You'd have to write your own "clear screen" function; I don't recommend it, but calculate the size of the dos shell display; print blank lines of the same amount as the size of the dos shell; then unfortunately your next text would be on the bottom.

    Basically, the key here is that "exec" executes the specified string command in a separate process.

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    So would it be somewhat correct to say that since notepad is an .exe java enabales us to execute this, and since cls is a probably a function within the dos shell it is unpermitted to be called upon? Ill try making my own
    display window with Frame or Dialog like you suggested i
    just have to check out the awt or swing classes to see how to do it. Well it's back to the books!

  8. #8
    Guest

    "exec" executes the specified string command in a separate process

    I'd agree with you on that statement about the exe and function within dos shell.

    When a new process is started, what is "cls" acting upon? I couldn't find cls.* under NT, so I suppose it is just a string that gets parsed in cmd.exe under NT and prints blank lines and locates the cursor to the top of the window.

    Strange. I thought I'd find cls.exe or cls.bat or cls.com. The main thing is to think of a new procees space. In fact, that is why you can get a Process object back if you needed one.

    But you should be able to call an exe and pass it some parameter. In that way, you might say you are calling a function within an exe, but the exe would have to be programmed to take a command line arg.

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