Runtime.Getruntime (). EXEC ()

xiaoxiao2021-03-06  38

Java Glossary: ​​EXEC

Last Updated 2005-01-11 by Roedy Green © 1996-2005 Canadian Mind Products

Java Definitions: 0-9 A B C D E F G H i J K L M N o P Q R S T U v W x y

You are here: Home: Java Glossary: ​​e Words: Exec.

EXEC

Runtime.Getruntime (). EXEC ("MyProg.exe") WILL SPAWN An External Process That Runs in Parallel with the Java Execution. In Windows 95/98 / ME / NT / W2K / XP / W2K3, You Must Use An Exprlicit

* .exe or

* .Com extension on the parameter. It is also best to fully qualify those names so that the system executable search path is irrelevant, and so you do not pick up some stray program off the path with the same name.

There are also overloaded forms of exec (),

Runtime.Getruntime (). EXEC ("Command.com / E: 1900 / c mybat.bat", null);

Runtime.Getruntime (). EXEC ("Command.com / E: 1900 / c mybat.bat", null, "c: // somedirectory);

The second argument can be a String [], and can be used to set environment variables In the second case,. "C: // SomeDirectory" specifies a directory for the process to start in If, for instance, your process saves files. To Disk, THIS FORM Allows You to Specify Which Directory They Will Be Saved IN.

Command Interpterto Run A

* .Bat,

* .Cmd,

* .html

* .BTM or URL you must invoke the command processor with these as a parameter. These extensions are not first class executables in Windows. They are input data for the command processor. You must also invoke the command processor when you want to use the

<> | Piping Options, Here's How, PRESUMING you are not interested in looking at the output:

// windows 98 command.com

Runtime.Getruntime (). EXEC ("Command.com / E: 1900 / c mybat.bat");

// Windows NT / W2K / XP cmd.exe

Runtime.Getruntime (). EXEC ("cmd.exe / e: 1900 / c mycmd.cmd"); // windows 98 with 4DOS

Runtime.getRuntime (). EXEC ("C: //4dos601//4dos.com / e: 1900 / c mybtm.btm");

// Windows NT / W2K / XP with 4NT

Runtime.getRuntime (). EXEC ("C: //4NT401//4NT.exe / E: 1900 / C mybtm.btm");

// linux Bash

String [] cmd = {"/ bin / bash", "-c", "rm / dira / *"};

Runtime.getRuntime (). EXEC (CMD);

Exec Only Understads

XXX.EXE AND Parameters. It know Nothout Theout Program Names WITHOUT THE

* .exe explicitly exercioned, | pipes, ,% environment parameter subsitution, or

* .bat,

* .cmd or

* .html files. for all those things you must spawn a commnd interpreter / shell script with an explcit

.exe extension and pass it the name of your executable as a parameter.

Windows and NT Will Let You Feed A URL STRING TO The Command Processor and It Will Find A Browser, Launch The Browser, And Render The page, E.g.

Runtime.Getruntime (). EXEC ("Command.com http://mindprod.com/ects.html);

Another Lower Level Approach That Does Not Require Extension Associations to Be Quite As Well Set Up UP:

Runtime.getRuntime (). EXEC ("Rundll32 Url.dll, FileProtocolHandler http://mindprod.com/ects.html");

Note That A URL IS NOT The Same Thing as a file name. You can point your browser at a local file with something like this: file: // localhost / e: /mindprod/jgloss.html or file: /// e | /mindprod/jgloss.html.

Composing Just The Right Platform-Specific Command To Launch Browser and Feed It a Url To Display Can Be Frustrating. You can use the browserlauncher package to do what for you.

Note That

Rundll32.exe url.dll, fileprotocolhandler file: /// e | /MINDPROD/jgloss.html

will not work on the command line because | is reserved as the piping operator, though it will work as an exec parameter passed directly to the rundll32.exe executable.With explicit extensions and appropriately set up associations in Windows 95/98 / ME / NT / W2K / XP / W2K3 you can offen bypass the command processly and invoke the file directly, evening * .bat.

SIMILARLY, For UNIX / Linux You Must Spawn The Program That Can Process The Script, E.G. Bash. However, You Can Run Scripts Directly with Exec if you do two things:

Start the script with #! Bash or whatver the interpreter's name is. Mark The Script File Itself with The Executable Attribute. Alternative Start The Script Interpreter, E.G.

Runtime.Getruntime (). EXEC (New String [] {"/ bin / sh", "-c", "echo $ shell"}

Communicating with the spawned process

Exec Returns A

Process Object That You Can Use to Control, Monitor or Wait for the Background Process Like this:

View

Process.getOutputStream lets you write out data that will be used as input to the process. Do not forget to include any "/ n" and "/ r" characters the process is expecting. Process.getInputStream lets you read in the data that Your Process Squirted Out to Stdout. in a Complex Case You Could Have Four Threads:

......................

The other cruder way to get the output from the execed program is to spawn a command processor and use the> or 2> redirection operator to direct stdout or stderr to a file. When the spawned program completes, read the file.

If you wait for the Process, you might want to do it on some thread other than the main AWT event processing one, ie create a new Thread, otherwise your app will not be able to process AWT events while you wait.In general you need To write to stdout and read from stdin with separate threads to avoid deadlocks. The deadlock occurs if you try to read and / or write more more a certin amount (Empirical IT IS Around 4K).

You can't Use EXEC from an applet OtherWise You Would Be Able To Wreck Havoc on The Client Machine by Spawning Format C :. If You Need To Do That, You Will Need To Have A Signed Applet With High Privilege.

Appletsif you are in y

Applet and all you are trying to is get a browser to render a page, you don't need

Exec. Simply Use

GetAppletContext (). showdocument (New URL ("http://domain/file.html");

SimplificationSthere IS Also

Marty Hall's Exec Class, Which Simplifies the Process of Using

EXEC AND TENDS to make the command more stable. currently, ONLY

Exec (String CMD) IS Supported, with a Workaround for

Exec (String CMD, NULL, STRING DIR)

Gotchasif you are using to c

Exec, Watch Out. Java Does Not Use The Dummy Parameter 0 Which Duplicates The Name of The Program in a Similar Way, Java

Main Methods Don't See That Dummy Parameter Either. You will NEED A

TEE Utility To See The Output Both on Screen and Captured to a file.

4DOS ¤

4NT ¤

Console ¤

JavaWorld Exec Article

TEE ¤

URL

转载请注明原文地址:https://www.9cbs.com/read-69966.html

New Post(0)