restarcik
restarcik

Reputation: 47

How to write simple script in batch to run some cmd's?

I am writing Server-Client program Java.

To test this program, I need to run several cmd windows. And this is very boring, especially if you just start work. The program operates in three sub-programs:

So I would like to write a script (I don't know how to do it because I've never written anything in batch) that:

Upvotes: 0

Views: 134

Answers (2)

Compo
Compo

Reputation: 38622

I would recommend using the Start command, as advised in the comments:

@Start "Customer" /D "E:\Java\MyApp" Cmd /K "java -cp (myJAR) app.Client"
@Start "Employee" /D "E:\Java\MyApp" Cmd /K "java -cp (myJAR) app.Node"
@Start "Server" /D "E:\Java\MyApp" Cmd /K "java -cp (myJAR) app.Server"

You could also, if the working directory is the same for each use this alternative:

@PushD "E:\Java\MyApp" 2>NUL && (
    Start "Customer" Cmd /K "java -cp (myJAR) app.Client"
    Start "Employee" Cmd /K "java -cp (myJAR) app.Node"
    Start "Server" Cmd /K "java -cp (myJAR) app.Server"
    PopD)

Or this:

@CD /D "E:\Java\MyApp" 2>NUL || Exit /B
@Start "Customer" Cmd /K "java -cp (myJAR) app.Client"
@Start "Employee" Cmd /K "java -cp (myJAR) app.Node"
@Start "Server" Cmd /K "java -cp (myJAR) app.Server"

Another alternative, if the intention is to ensure that the ClassPath search includes E:\Java\MyApp or E:\Java\MyApp\myJAR.jar is to add it directly to the commands:

@Start "Customer" Cmd /K "java -cp E:\Java\MyApp\myJAR.jar app.Client"
@Start "Employee" Cmd /K "java -cp E:\Java\MyApp\myJAR.jar app.Node"
@Start "Server" Cmd /K "java -cp E:\Java\MyApp\myJAR.jar app.Server"

As also advised in the comments, to read the help and usage information for the Start and Cmd commands, open a Command Prompt window and enter start /? and cmd /? respectively.

Upvotes: 1

wasif
wasif

Reputation: 15488

Use this:

@Echo Off
cmd /k "pushd E:\Java\MyApp & java -cp (myJAR) app.Server"
cmd /k "pushd E:\Java\MyApp & java -cp (myJAR) app.Node"
cmd /k "pushd E:\Java\MyApp & java -cp (myJAR) app.Client"

Upvotes: 1

Related Questions