Running a multiple jar files in a batch file - java

I'm trying to run a batch file that included multiple jar files, Batch file includes 3 Jar files which executes one after another in one window, My batch file is working correctly for one record which fetches data from excel sheet.
Consider a scenario in which i have 5 records and i wanted to run the batch file in the manner like for 1st record->1st jar prog executes then 1st record->2nd jar file and finally 1st record->3rd jar file executes. Then this loop continues for the second record and likewise. Could anyone please help me to modify the below script which runs in loop and i want to save a executed results in a separate text file.
My script is below:
REM Run first and finish ...
java -jar first.jar
REM .. then start number two.
java -jar second.jar
REM .. then start number three.
java -jar third.jar
Kindly help!

You cloud do something like this that waits until the execution of one jarfile is done.
#echo off
java -jar 1.jar
pause
java -jar 2.jar
pause

If you want to run the jars sequentially, you can write a .bat file containing the following;
#echo off
java -jar first.jar
java -jar second.jar
java -jar third.jar
If you want to tun the jars simultaneously, you can write the .bat file as follows;
#echo off
start java -jar first.jar
start java -jar second.jar
start java -jar third.jar
START command will start running the jar in a new window.

You'll need something like
FOR %%A in (1 2 3 4 5) DO (
java -jar first.jar
java -jar second.jar
java -jar third.jar
)
That should execute the three jar consecutively five times. I didn't actually test this, but it should give you the idea. Here is an article on FOR loops syntax in batch files: http://www.robvanderwoude.com/for.php

Related

How to get jar name into startup script within bat(batch) file

I have a bat file I run through command prompt to deploy a java app locally for local testing on my machine:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-version.jar
exit
To run this bat file, I use the following command:
start batFileName.bat
However, the next time the version changes on this jar, the bat file will not work, because the version is out of sync. This results in myself having to change my bat file each time the version is updated.
Is there a way to pass in a the version when I run the start command through command prompt to use as the jar name? This way when I run my bat file, I can just pass in the name of the jar at that time to run the java application? If so how would I pass that version into the bat file and how would I use that parameter?
In your script, replace the version part of the jar file name with an argument replacement parameter:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-%1.jar
Do not start the program using java -jar . Change the start up script
include the folder where you jar file is present into class path with wild card, like:
java -cp path\to\jar*
call the main class in your jar file. I suppose the main class does not change so often as versions of the jar file?
The whole command line will look like this:
java -cp path\to\jar* com.something.foo.bar.Main
JVM will load your jar whatever its name is, and will find the main class and will start it if it has "main" method.

Get rid of the script file that starts a jar

I have 3 files :
MyApp.jar
start.cmd
start.sh
Here is what's in the start files :
java -cp MyApp.jar -Xms256m -Xmx1024m com.companyname.launch.Launcher someArgs -DsomeParameter=true
As you can guess, I have to start the application by executing the correct start file, depending on the OS I'm using.
Is there a simple solution to get rid of the start files, and have only a system independent executable jar file ?
You can run this from Java.
Have a FirstMain which doesn't depend on anything. e.g. can be run without command line arguments. This main start another java program for the same JAR starting the RealMain with all the command line arguments you need.

Looping through jar in bash and storing console o/p in csv file

I have a shell script as follows. For some reason the op.csv file is not generated if there are multiple json files, i.e. java -jar is run multiple times within the for loop. However, if java -jar is run only once then the op.csv files gets created fine. Why is that?
for filename in $curr_dir/op/*.json; do
java -jar SCSVGEN.jar "$filename" >> 'op.csv'
sleep 2
done

How to launch a jar file on windows startup from the registry?

I am trying to launch a jar file on Windows Server 2008 R2 startup.
I tried to add a key\value to
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
I tried as a value several alternatives:
java -jar c:\jcm\jcm.jar
"java -jar c:\jcm\jcm.jar"
c:\program files(x86)\java\jre1.8.31\bin\java.exe -jar c:\jcm\jcm.jar
"c:\program files(x86)\java\jre1.8.31\bin\java.exe -jar c:\jcm\jcm.jar"
"c:\program files(x86)\java\jre1.8.31\bin\java.exe" -jar c:\jcm\jcm.jar
But none of them launched the program.
Can you advise please?
EDIT: Fixed exe to jar of course
You probably have a problem in the command line. I think it should be something like that last row that you have used but with jar and not exe.
"c:\program files(x86)\java\jre1.8.31\bin\java.exe" -jar c:\jcm\jcm.jar
You just need to test it in command line first and if it works it will work in the registry. You can also make a .bat file to start the java program and copy that to windows Startup Folder instead of using the registry.
The startup folder you can find here:
http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7
If it is Windows 10 then you may try this way-
Create a batch file e.g. (my-batch.bat with below statements.)
#echo off
title my-app batch script!
echo my-app welcomes you!
:: If your JAR contains GUI then use-
start javaw -Xmx200m -jar D:\Softwares\JAR\my-app-v1.0.jar
:: Otherwise use-
start java -jar D:\Softwares\JAR\my-app-v1.0.jar
Copy this my-batch.bat file at
C:\Users\pc\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Restart your PC to execute your JAR on startup.
Now, You may check your running JAR using Command Prompt with below commands-
1. jps -l
Output will look like-
8120 sun.tools.jps.Jps
13276 D:\Softwares\JAR\my-app-v1.0.jar
2. tasklist /V

open 2 jar files on linux command

I need to open run two jar applications in the same time
I know that to run a jar file you have to type
java -jar app1.jar
but I need to terminate the current process to run
java -jar app2.jar
How can I open app2 without closing app1 ?
Execute this bash command
( java -jar app1.jar ) &
( java -jar app2.jar )
The commands will execute in parallel subshells
You can run one, push it into the background, and then run the other.
Run the first command, press ctrl-z and then type bg.
This runs the command in the background, leaving your command line available for you to call you next command.
http://www.thegeekstuff.com/2010/05/unix-background-job/

Categories