Calling Java application from bat file and continue after its done? - java

I am calling at batfile App.bat that calls a Java application:
echo %DATE%_%TIME% START >> log.log
App.bat import
echo %DATE%_%TIME% END >> log.log
Content in the App.bat
%JAVACMD% %JAVA_OPTS% %EXTRA_JVM_ARGUMENTS% %APP_OPTS% -classpath %TS_CLASSPATH% org.App %CMD_LINE_ARGS%
where org.App is located in the App.jar file.
But after the App.bat bat file is done, the last line in the wrapper bat file is never executed:
echo %DATE%_%TIME% END >> log.log
How do I execute the last line after the call to the Java application have returned?

Your example works well for me, except that I have changed the Java class execution part.
Callme.java
public class Callme {
public static void main(String args[]) {
System.out.println("Called JavaApp");
}
}
CallMe.bat
echo %DATE%_%TIME% START >> log.log
java Callme
echo %DATE%_%TIME% END >> log.log
log.log after execution of bat file
07/07/2015_13:39:10,79 START
07/07/2015_13:39:11,31 END
OK, now that you have changed your question, can you try the below and check if you are getting end date?
echo %DATE%_%TIME% START >> log.log
call App.bat import
echo %DATE%_%TIME% END >> log.log

As you say that you don't have any problem with the Java program by calling the program as
MyJavaApp import
, below script has to work..
echo %DATE%_%TIME% >> "d:\New folder\log.log"
MyJavaApp import
echo %DATE%_%TIME% >> "d:\New folder\log.log"
Double check whether your Java program has completed. You can also double check whether the Java program is completed by passing a return code using
System.exit(anyInteger);
and get the code in batch and check for appropriate program status.

Since I am a batch programmer I will tell you this (pretty sure it will work 100%.).
Remove the last line from the batch file, and in the java file make a code to start a file named "ENDLOG.bat".
Then make a file named "ENDLOG.bat"/A batch file named "ENDLOG".
Now remember the last line you removed from that batch file? Place in that batch file.

Related

Not printing anything after for loop in a batch script

Below is my batch script which is not executing the line after for loop end. It's not print echo end or the line after that. The line asciidoctor-pdf C:\Users\abc\Conversion_to_PDF\OutputFiles\*.adoc is causing it. But I am not sure with what the problem is.
#echo off
echo # Starting job
java -jar C:\Users\abc\Conversion_to_PDF\swagger2markup-cli-1.3.1.jar convert -i C:\Users\abc\Conversion_to_PDF\HIP-ProviderManagement-1.0.0-swagger.json -d C:\Users\abc\Conversion_to_PDF\OutputFiles
chdir /d C:\Users\abc\Conversion_to_PDF\OutputFiles
for %%A in (*.adoc) do (
asciidoctor-pdf C:\Users\abc\Conversion_to_PDF\OutputFiles\*.adoc
echo %%A
)
echo # end
C:\Users\abc\Downloads\sejda-console-3.2.3\bin\sejda-console merge -f C:\Users\abc\Conversion_to_PDF\OutputFiles\*.pdf -o C:\Users\abc\Conversion_to_PDF\OutputFiles\merged.pdf
You need
call asciidoctor-pdf ....
since asciidoctor-pdf is a batch file. The call means "execute this, then return to the next statement". Without the call, it means "go to this batchfile" and it is not told to return to the original (the "caller")

Run Java Code Provided in Argument to CLI

I was wondering if it is possible to pass code as an argument to the Java CLI and have it compile and run the code in memory instead of having to write it to a file, compile the file, run the compiled file, then delete everything. If not, is there a way to emulate this behavior in a UNIX environment (Linux/macOS)?
Example:
> java --code 'public class Main { public static void main(String[] args) { System.out.println("Hello, world."); } }'
You can do it usin bash.
Basicaly it will look like this:
#!/bin/bash
while getopts ":c" opt; do
case $opt in
c)
echo $2 > ./Main.java
javac Main.java
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
It takes the argument c as code, saves it to temporary file and compiles. You can delete if it is unnecessary by adding rm Main.java later.

Cron job not running the jar

This is my Java file for which I have created Delete.jar
import java.io.*;
public class Delete {
public static void main(String[] args)
{
try{
int i =1;
while(i<5){
File directory = new File("downloads");
System.out.println("I am running");
for(File file: directory.listFiles()) file.delete();
i++;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
This is my Script to run the jar file if it is not running
#!/bin/bash
processid=`pgrep -f 'Delete.jar high'`
echo "Processes:"$processid
if [ -n "$processid" ]
then
echo "Process is running. No action will be taken"
else
echo "Process is not running. Executing ResponseHandler-fast now !"
cd /home/ubuntu/;
java -jar Delete.jar high
fi
This is line I have added to my crontab -e
* * * * * sh /home/ubuntu/check.sh
I rebooted my System I was expecting that my script will run check that jar is not running and it will run it but it is not doing so.
What I am doing wrong here.
If I execute ps after 2 -3 minutes still I am not getting java as an entry.
Thanks.
Please send output of your shell script to a log file as shown below(make changes to your crontab entry):
* * * * * sh /home/ubuntu/check.sh >> /home/ubuntu/output.log 2>&1
In this way, you will know what exactly is being run and then finding the exact cause will become easier.
Cron doesn't magically make the program "run forever". Start the program manually. It will probably take 1-2 seconds to run, then exit. This is exactly what happens when running with cron, as well. So, unless you run ps the second your program gets started, you won't see anything in the process list.
Your loop 1..5 won't help, as after the files are deleted in the first round, the rest is effectively a no-op.
Did you try running /home/ubuntu/check.sh manually? May be it's not finding the jar file or even java program.

How can I get output from subprocess.check_output if the subprocess is a java program(with Command-line arguments)?

I have the following java code: (HelloWorld.class is in bin folder):
package Hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World" + args[1]);
}
}
I want to call this java code from a python script and pass some command-line arguments to it.
So my python code is:
cmd = ["java","-classpath","bin/","Hello.HelloWorld","arguement1","arguement2"]
try:
print subprocess.check_output(cmd,stderr=subprocess.STDOUT)
except: subprocess.CalledProcessError:
print ('calling '+ ' '.join(cmd) +' failed\n')
If I run this code, I will get the no output from java code, and also get output "calling java -classpath bin/ Hello.HelloWorld arguement1 arguement2 failed".
But if I run:
java -classpath bin/ Hello.HelloWorld arguement1 arguement2
in terminal, the java code will print the string.
So where is wrong of my python code?
You don't see the output because both stdout and stderr are captured by check_output(stderr=STDOUT) and java exits with a non-zero exit status that leads to the exception and that is why you see ".. failed" message.
To get subprocess' output in the exception handler except CalledProcessErrror as e:, access e.output attribute.
If you don't need to capture the output then just call call() instead:
import subprocess
subprocess.check_call(cmd)

Perl calls java - How do I prevent an exception display?

I have a perl CGI that calls a java application which in turn checks a Mysql database. If I search for an entry that does not exist, the java application displays an exception handler message on the server (requires an X display window). The exception is straight forward and understandable, however must be clicked to close it, at which point the perl CGI can continue. Customers of course cannot (and should not) see the exception message.
My question is.. how can I prevent the exception message from displaying on the server window and preventing the CGI from continuing? Is there a way to close the message from perl? I have control over the perl script, but not the java application I call.
$ENV{'DISPLAY'} = 'myserver:0.0';
$testline = system("java -Dby.product=true -jar javaApp.jar $version status>mytest.txt") >> 8;
if $version doesn't exist, I get the exception.
I pipe the results to a file for later file handling in perl
Thanks.
Rocky.
=====================
Thanks.
I added this...
$ENV{'DISPLAY'} = 'server:0.0';
use IPC::Open2;
use POSIX ":sys_wait_h";
$pid = open2(\*CHLD_OUT, \*CHLD_IN, "java -Dby.product=true -jar javaApp.jar $version status>mytest.txt 2>/tmp/java_error.$$");
sleep(5);
kill('TERM', $pid);
If I use a known value in the database, it works fine, as before.
If I search a nonexistent value, the java message still pops up.
Without the sleep line, the java message does NOT popup. In other words it looks like the pid is killed, but so quickly that the result does not get fed into mytest.txt. I thought the sleep function would give some time for the java app to work and then the kill would remove the popup message. But this does not happen.
It seems likely I will have to request changes to the java application so that it does not display a message on screen in the server.
Corrected and extended
Try
system("java -Dby.product=true -jar javaApp.jar $version status 2>/dev/null >mytest.txt");
It redirects the stderr of java to nowhere. Or redirect to a file (like 2>/tmp/java_error.$$) to save it debugging the error.
If it is on windows use 2>nul.
Or use IPC::Open3 and process both input file handles as You like.
More detailed. I created a simple a.java code which writes to stdout, stderr and throws an exception if an arg is defined:
public class a {
public static void main(String[] args) {
System.out.println("STDOUT");
System.err.println("STDERR");
if (args.length > 0) { int i = 1/0; }
}
};
I compiled and run it (don't care about the gcj warning):
$ gcj -C a.java
$ gij -cp . a
STDOUT
STDERR
$ gij -cp . a x
STDOUT
STDERR
Exception in thread "main" java.lang.ArithmeticException: / by zero
at a.main(a.java:5)
$ gij -cp . a >/dev/null
STDERR
$ gij -cp . a x 2>/dev/null
STDOUT
So the stack dump is written to the stderr as expected, so it can be redirected.
$ perl -e 'system("gij -cp . a x 2>/dev/null")'
STDOUT
The example program with IPC::Open3:
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open3 'open3';
use Symbol 'gensym';
my ($fcin, $fcout, $fcerr);
$fcerr = gensym;
my $pid = open3 $fcin, $fcout, $fcerr, "gij -cp . a x";
my #out = <$fcout>;
my #err = <$fcerr>;
my $err = waitpid $pid, 0;
print "Exit:", ($err >> 8), "\n";
print "OUT: #out\n";
print "ERR: #err\n";
Output:
Exit:72
OUT: STDOUT
ERR: STDERR
Exception in thread "main" java.lang.ArithmeticException: / by zero
at a.main(a.java:5)
Even the man page of IPC::Open3 suggests to use the IPC::Run package. I tried, but it is not the part of the normal distribution. So You can install it from CPAN if You wish.

Categories

Resources