I am trying to execute command line arguments via Java. For example:
Active3 years, 6 months ago
The above opens the command line but does not execute
cd or dir . Any ideas? I am running Windows XP, JRE6.
Use notepad to write java program and then set the java path in command prompt. Then run java class file from command line. By this way you can easily run java program in cmd using notepad.
(I have revised my question to be more specific. The following answers were helpful but do not answer my question.)
Madhawa Priyashantha
8,58577 gold badges2323 silver badges5252 bronze badges
joejoe
6,8603535 gold badges8484 silver badges127127 bronze badges
9 Answers
The code you posted starts three different processes each with it's own command. To open a command prompt and then run a command try the following (never tried it myself):
Chulo
5,9291111 gold badges4747 silver badges6969 bronze badges
Peter KnegoPeter Knego
77k99 gold badges115115 silver badges142142 bronze badges
I found this in forums.oracle.com
Allows the reuse of a process to execute multiple commands in Windows: http://kr.forums.oracle.com/forums/thread.jspa?messageID=9250051
You need something like
SyncPipe Class:
tvanfosson
444k8686 gold badges660660 silver badges762762 bronze badges
PepePepe
If you want to run several commands in the cmd shell then you can construct a single command like this:
Execute Java Code In Cmd
This page explains more.
Vincent RamdhanieVincent Ramdhanie
91.4k2121 gold badges126126 silver badges176176 bronze badges
Every execution of
exec spawns a new process with its own environment. So your second invocation is not connected to the first in any way. It will just change its own working directory and then exit (i.e. it's effectively a no-op).
If you want to compose requests, you'll need to do this within a single call to
exec . Bash allows multiple commands to be specified on a single line if they're separated by semicolons; Windows CMD may allow the same, and if not there's always batch scripts.
As Piotr says, if this example is actually what you're trying to achieve, you can perform the same thing much more efficiently, effectively and platform-safely with the following:
Community♦
Andrzej DoyleAndrzej Doyle
87.7k2828 gold badges177177 silver badges219219 bronze badges
![]()
Try this link
![]()
You do not use 'cd' to change the directory from which to run your commands. You need the full path of the executable you want to run.
Also, listing the contents of a directory is easier to do with the File/Directory classes
PiotrPiotr
2,42355 gold badges2525 silver badges4444 bronze badges
Each of your exec calls creates a process. You second and third calls do not run in the same shell process you create in the first one. Try putting all commands in a bat script and running it in one call: Carles BarrobésCarles Barrobés
rt.exec('cmd myfile.bat'); or similar
9,62333 gold badges3737 silver badges5757 bronze badges
This because every
runtime.exec(.) returns a Process class that should be used after the execution instead that invoking other commands by the Runtime class
If you look at Process doc you will see that you can use
Example Java Program Codes
on which you should work by sending the successive commands and retrieving the output.
JackJack
113k2626 gold badges196196 silver badges298298 bronze badges
Writing to the out stream from the process is the wrong direction. 'out' in that case means from the process to you. Try getting/writing to the input stream for the process and reading from the output stream to see the results.
Kelly S. French
Steps To Execute Java Program In CmdKelly S. French
10.8k88 gold badges5151 silver badges8888 bronze badges
As i also faced the same problem and because some people here commented that the solution wasn't working for them, here's the link to the post where a working solution has been found.
Check Java In Cmd
Also see the 'Update' in the best answer for using Cygwin terminal Free sharepoint 2013 templates.
Community♦
Compiling Java Program In Command Promptsinge3Compiling Java Code In Cmdsinge3
1,19011 gold badge1313 silver badges4040 bronze badges
Not the answer you're looking for? Browse other questions tagged javaexec or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |