Hey

I would like to do a ".bat' type of command in Java: Something automated. Im trying to run a program that is sort of shell itself so it just need to input certain words/letters into the command line box. Lets see if I can explain it better with a theoretical example:


[start of cmd]
C:\>echo hello
hello

C:\>someshell
[start of someshell: It has a command deleteall that deletes everything. It has a command bye that exits someshell]
> deleteall
Are you sure you want to delete all? [Y/N] Y
Everything has been deleted!
> bye
[end of someshell]
C:\>exit
[end of cmd]


As you can see the things I would have to type in the prompt if in cmd are:

echo hello
someshell
deleteall
Y
bye
exit

The biggest problem is the "Y" I believe, since cmd invokes someshell which at the same time and during invokes deleteall.

How could I do this in Java? Thanks.

Hey

I would like to do a ".bat' type of command in Java: Something automated. Im trying to run a program that is sort of shell itself so it just need to input certain words/letters into the command line box. Lets see if I can explain it better with a theoretical example:


[start of cmd]
C:\>echo hello
hello

C:\>someshell
[start of someshell: It has a command deleteall that deletes everything. It has a command bye that exits someshell]
> deleteall
Are you sure you want to delete all? [Y/N] Y
Everything has been deleted!
> bye
[end of someshell]
C:\>exit
[end of cmd]


As you can see the things I would have to type in the prompt if in cmd are:

echo hello
someshell
deleteall
Y
bye
exit

The biggest problem is the "Y" I believe, since cmd invokes someshell which at the same time and during invokes deleteall.

How could I do this in Java? Thanks.

So you want a java program to act as a console much like command prompt? you want to be able to type echo hello in your java console and it must output:"hello" have i understood you correctly?

You can run batch jobs as at the command prompt frim Java by using ProcessBuilder (Google for examples and details), but you may get a far better solution all round by embedding a JavaScript script in your Java program. Check out scripting language support in Java (Java 1.6 or later).

You can run batch jobs as at the command prompt frim Java by using ProcessBuilder (Google for examples and details), but you may get a far better solution all round by embedding a JavaScript script in your Java program. Check out scripting language support in Java (Java 1.6 or later).

if JamesCherrill is on track with what you asked then i would also suggest maybe just writing to a batch file and executing it, thats what i did in my one app:

try {
                    PrintWriter pw = new PrintWriter("test.bat");
                    pw.write("@echo off" + "\r\n");
                    pw.write("echo Hello world"+"\r\n");
                    pw.write("pause"+ "\r\n");
                    pw.write("exit");
                    pw.flush();
                    pw.close();
                    Runtime.getRuntime().exec("cmd.exe /c start test.bat").waitFor();
                    new File("runclass.bat").deleteOnExit();
                } catch (Exception ex) {
                  ...
                }
            }

Yes, you can use Runtime.exec to run a program or batch file, but it's limited in how you can configure the environment for your program/batch.
ProcessBuilder was introduced in Java 1.5 specifically to address the limitations of Runtime.exec and is the preferred choice for all new code.
Currently the legacy implementation of Runtime.exec has been scrapped, and now it's just a cover for ProcessBuilder anyway... here's the Java 1.7 API source code for Runtime.exec:

public Process exec(String[] cmdarray, String[] envp, File dir)
        throws IOException {
        return new ProcessBuilder(cmdarray)
            .environment(envp)
            .directory(dir)
            .start();

See also https://sites.google.com/site/javaerrorsandsolutions/home/processbuilder

commented: Hmm very nice +7

So you want a java program to act as a console much like command prompt? you want to be able to type echo hello in your java console and it must output:"hello" have i understood you correctly?

In the end, yes. This is something similar to what I want to do.

You can run batch jobs as at the command prompt frim Java by using ProcessBuilder (Google for examples and details), but you may get a far better solution all round by embedding a JavaScript script in your Java program. Check out scripting language support in Java (Java 1.6 or later).

Javascript is not a strong point on me so I rather leave it at just Java

if JamesCherrill is on track with what you asked then i would also suggest maybe just writing to a batch file and executing it, thats what i did in my one app:

From with this is that I cant answer things from other shells right? Thats why I put the "someshell" example. I know I can print out echo world, thats not a problem.

In the end, yes. This is something similar to what I want to do.


Javascript is not a strong point on me so I rather leave it at just Java


From with this is that I cant answer things from other shells right? Thats why I put the "someshell" example. I know I can print out echo world, thats not a problem.

What do you mean by you cant answer things from other shells?

If I start another shell (such as bash or "someshell") from CMD and it supports the command "deleteall" and I have to answer "Y" to confirm, that is the problem: Can I answer "Y" from my C# program?

If I start another shell (such as bash or "someshell") from CMD and it supports the command "deleteall" and I have to answer "Y" to confirm, that is the problem: Can I answer "Y" from my C# program?

uhm yes im sure you can, because it wont proceed with the command until its gets a 'Y' look into what jamescherrill was saying you can use input and outputstreams, to get the replies from the shell. But now your talking c#? uhm youare definitely not going to get the best c# answers here

If I start another shell (such as bash or "someshell") from CMD and it supports the command "deleteall" and I have to answer "Y" to confirm, that is the problem: Can I answer "Y" from my C# program?

But i just thought of what you said. If you create a shell script and don't supply a /y switch it will cause the console to wait and ask for input(which is where you are asking how do you respond to it), but if you supply the /y switch it wont ask and it would just delete the file. The same will be for other shells, why do you want to give a command thats going to wait for another command as a response? because as i see it, say now your app allows a person to delete a file, your app would then let them select the file to delete, then your app would ask if they are sure, and if they click yes, you will then send the command with the correct switches(to not produce any output to the user and aslo to not produce a response you have to act on). The way i see what your asking is you want them to say delete this file, and immediately you delete it but you dont add a /y switch so its going to ask then if they are sure, so why not ask them before if they are sure they want to delete the file, and then supply correct switches to make the command execute with no interference from the console itself?

uhm yes im sure you can, because it wont proceed with the command until its gets a 'Y' look into what jamescherrill was saying you can use input and outputstreams, to get the replies from the shell. But now your talking c#? uhm youare definitely not going to get the best c# answers here

Im trying to see which language is easier to do this in.
C# isnt giving me much choices...

But i just thought of what you said. If you create a shell script and don't supply a /y switch it will cause the console to wait and ask for input(which is where you are asking how do you respond to it), but if you supply the /y switch it wont ask and it would just delete the file. The same will be for other shells, why do you want to give a command thats going to wait for another command as a response? because as i see it, say now your app allows a person to delete a file, your app would then let them select the file to delete, then your app would ask if they are sure, and if they click yes, you will then send the command with the correct switches(to not produce any output to the user and aslo to not produce a response you have to act on). The way i see what your asking is you want them to say delete this file, and immediately you delete it but you dont add a /y switch so its going to ask then if they are sure, so why not ask them before if they are sure they want to delete the file, and then supply correct switches to make the command execute with no interference from the console itself?

(Just in case, all I wrote was a example. Nothing real. That being said....)

Will this work in all cases? And Im not sure we are understanding the same thing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.