Understanding syntax for running Java program in command line - java

I'm trying to understand this batch job file, there are two and the first is returning an exit code of 0:
set JAVA_HOME="C:\Program Files(x86)\Java\jre1.8.0_221\bin"
%JAVA_HOME%\java -Xms125M -Xmx512M -Djava.ext.dirs=lib org.pg.test.ListOutput > output.txt 2>exception.txt
And this second one is returning an exit code of 1:
set JAVA_HOME="C:\Program Files(x86)\Java\jre1.8.0_221\bin"
%JAVA_HOME%\java -Xms125M -Xmx512M -Djava.ext.dirs=lib org.pg.test.ListOutput 1 0 > output.txt 2>exception.txt
What does the 1 & 0 after the file name mean exactly? Why is this making it return an exit code 1?
Both files run normally, with a successful output in output.txt.
Trying to google the exact syntax this is in (batch files were not written by me) but maybe I'm searching for the wrong thing.

The documentation for the java command is found
here for JDK/JRE version 18 (a fairly recent version), and
here for JDK/JRE version 1.8 for Windows, which appears to be what you're using.
The 1 and the 0 after the class name are arguments that will be passed to the Java program when it runs. As for why this causes the program to exit with a particular exit code, that depends on what the program does. It's impossible to answer that without seeing the source code of the program.

Related

Difference in execution speed between eclipse and my compiled program

I have a strange problem. I noticed that on operations where there are variable manipulations in for loops with a lot of iteration, there were big differences in terms of execution time between these two situations:
The program is launched by Eclipse
The program is launched in terminal with a "java -jar ..."
The same loop takes 1 hour with the terminal and 1 minute under Eclipse! (it is about reading a CSV file and putting the columns in a table)
I specify that my program has these VM parameters:
-Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.http.auth.proxying.disabledSchemes="" -Dsun.net.http.allowRestrictedHeaders=true -Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" -Xms2g -Xmx40g -Dhttps.protocols="TLSv1,TLSv1.1,TLSv1.2"
But the problem doesn't come from there I think. To validate this point I proceeded as follows:
I went in the VM arguments of the program launcher in Eclipse and checked that they were there
I looked at the default JVM used by Eclipse in the general settings and I used this path to launch my program in terminal with this command :
"C:\Program Files\Java\jdk-11.0.11\bin\java.exe" -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.http.auth.proxying.disabledSchemes="" -Dsun.net.http.allowRestrictedHeaders=true -Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" -Xms2g -Xmx40g -Dhttps.protocols="TLSv1,TLSv1.1,TLSv1.2" -jar run.jar
Is there anything that can explain this performance difference? I would like to get it back so that my program can run faster.

Spoon is not supported on this hosttype : MINGW64_NT-6.1

I'm trying to open entaho Spoon on a Windows 7 machine, but it's not openning.
When I run SpoonDebug, the SpoonDebug.txt brings me this error:
Spoon is not supported on this hosttype : MINGW64_NT-6.1
What should I do?
when I run Git Bash, MINGW64_NT is the name on the header of the program.
I see that you are using the GitBash terminal to run the pentaho sh files (I know this is the best way we can probably simulate a linux envt on a windows system , even i do the same thing )
What you have to get rid of this validation is
Go to spoon.sh , You could see case statements for Linux, AIX etc..
starting with case uname -s in
You basically need to add one condition for MINGW64_NT-6.1
You can even copy paste the linux settings as-is and replace the Linux) with MINGW64_NT-6.1). This is the tweak which is working for me over years now.
The error is you are trying to run spoon.sh on windows 7 using cygwin or any of shell emulator for windows.
This output as you wrote:
MINGW64_NT-6.1
Is just output from sh file - output of
uname -s
What actually just happens - spoon.sh make attempt to guess the platform you are using and based on platform name - choose correct swt implementation. Since you are on win7 correct one should be with /win in path. 64 or 86 depending on your core. spoon.sh trying to guess only linux/osx based implementations and will not go to win swt folder.
So on win should run Spoon.bat for SpoonDebug.bat. spoon.sh only for unix/linux and seems will not guess the win swt even running in shell simulator that able to execute sh files.

how to use command line to check abnormal termination of Java Class

I have simple Java Class with a main function. I run this class with command-line 'command' which is present in the bat file, I have scheduled the bat file to run after every 2 hours.
This .class uses some resources which some time are not available at the request and due to timeout the .class terminated abnormally.
What I want to is if the bat terminates abnormally is there a way to check it and run it again until it runs properly.
Secondly if the bat terminates abnormally, is there a way to roll back the operations this .class performed before terminating. Lets say it was updating some text in a text file. Is there a way to roll it back.
There are many causes of abnormal termination.
There is a heuristic that you can adopt here:
an exception finding its way up to the main thread and which is not caught causes the JVM to terminate with an exit code of 1;
by exception above, this includes such abnormalities as OutOfMemoryError; understand that by "an exception" in the statement above, it is "any instance of Throwable"; Error is a subclass of Throwable, OutOfMemoryError is a subclass of Error;
if you wish for your program to terminate for other anomalous conditions, use System.exit() and ensure that the error code is something other than 1 because of the above.
Secondly if the bat terminates abnormally, is there a way to roll back the operations this .class performed before terminating. Lets say it was updating some text in a text file. Is there a way to roll it back.
No generic way; this really depends on your program itself. If you are talking about file I/O, there exist several paradigms to ensure that the original contents of files do not end up corrupted, at least as far as the programming language itself is concerned; more severe corruption scenarios are delegated to the filesystem itself.
Side note about exit codes... At least with Unix systems, you should be wary that in fact "valid" exit codes range from 0 to 127; yes, that is 7 bits. The 8th bit is there to signal that the process termination is due to a signal, in which case the lower 7 bits are to be interpreted (understand, "read") as the signal number which triggered program termination.
If you want to have a .bat run continuously until it runs properly after crashing you can achieve this by creating a windows service. When you create the windows service you can specify what happens after crashing, how long to wait before running the script again, and how many times it should try to run the script again.
As far as reverting what a script has done to a textfile, there are multiple solutions:
Have your script make a copy of the file, you can rename it original_new, and manipulate the new copy. At the end of the script you can have it delete the original and replace it with the copy by changing original_new to original. In case the program crashes, you will have two files, so at the beginning of your script, check to see if original_new already exists, if it does, delete it then copy the original.
You can keep a record log of everything the script does and on the startup of the script it checks the log and if the log is incomplete then the script first reverts any of the logs until it reaches the last complete sign and then runs the actual intension of the script.
It sounds like the bat fails because the java class job fails. If failure of the java job can be detected inside the batch file, you should be able to do everything you want from there.
First, to confirm the java job raises an error the batch identifies, add the following after the line that runs the java class job and test it. You should see errorlevel 0 with success and >0 with error:
REM THIS IS LINE THAT RUNS JAVA JOB
echo errorlevel is "%errorlevel%
pause
Assuming the above returns accurate exit info, here's some code that implements the other answers and reruns the java job.
To continuously rertry the java job until it runs successfully, add a label and a goto statement:
REM Add following label to a line before the java class job
:runhere
REM THIS IS LINE THAT RUNS JAVA JOB
rem Check errorlevel and retry if failure
If not %errorlevel%==0 goto runhere
Exit
To rertry the job n times and then quit even if not yet successful, create backup file as #niebloomj suggests and set a counter. This tries to run the job 10 times.
set COPYCMD=/Y
set Outputfile=path\file.ext
set Outputbak=path\file_bak.ext
copy "%Outputfile%" "%Outputbak%"
Set /a count=0
:runhere
REM THIS IS LINE THAT RUNS JAVA JOB
If %errorlevel%==0 (
:: now have new good output file
del "%Outputbak%"
exit
)
rem Check count and retry
If %count% LSS 10 (
set /a count+=1
goto runhere
)
rem When count = the "check" number above, replace old file and quit
copy "%Outputbak%" "%Outputfile%"
del "%Outputbak%"
echo Java job failed after retries. "%Outputfile%" not updated.
pause
Exit

Unrecognized option: - Could not create the Java virtual machine

I am trying to run my .jar file on a linux server.
These are the commands I'm running:
JAVA_HOME=/app/local/opt/jdk/packages/jdk1.6.0_30/
export PATH=$JAVA_HOME/bin:$PATH
cd folder
nohup java -jar program.jar &
When ran on Friday, this was working perfectly. However, today I am getting:
Unrecognised option: - Could not create the Java virtual machine.
In my nohup.out
If I run java -v after setting the path I get the same problem. Can anyone suggest anything? Really confused about this.
Thanks for any help on this.
Usually, when we get this kind of exception ("Unrecognised option" which causes the "Could not create the Java virtual machine"), the JVM displays the unrecognized option, like this:
Unrecognised option: -[option details] Could not create the Java virtual machine.
the fact that you have no details makes it seem as though you just have a space (" ") in your command.
Take a look again to make sure.
I've got this failure running maven on Win10 where the pom.xml is located in a folder named something like C:\dir\OneDrive - bar\workspace\project.
Moving the project to a different folder solved it.
First of all, there is no -v option on the java command.
(Reference: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html ... Java 6 version ... 'cos that is what you are using. Try java -help for the short list of options.)
So it is expected for java -v to fail.
As an experiment I tried this (with an OpenJDK Java 8 JDK on Fedora 22):
$ java -foop
Unrecognized option: -foop
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
$
As you can see, the "unrecognized option" message tells me what option it doesn't understand. Looking at the message in your question, we see that it is saying it is "-". That doesn't appear to match the command line in your question, but on the other hand the command line in your question does not have any invalid options.
Another possibility, I guess, is that you have a shell alias called java. That would take precedence over any commands on the shell's command path.
The other thing is that you are running an ancient version of Java. Java 6 has passed end-of-life, and the last publicly released version is update 45. You are running update 30 which was released in December 2011. There have been LOTS of security patches since then. I'd recommend upgrading to Java 8 ... and making sure that you keep it up to date.
for java 8 run 'java -version'
for java 9 or 9+ run 'java --version'
Had the same issue under Linux/Ubuntu 20.04.
I created a bash to run my jar and used some variables in the file
# This line below shows the error
$JAVA_PATH "-jar $JVM_ARGS $JAR_PATH"
# This line below runs without issue
$JAVA_PATH "-jar" $JVM_ARGS $JAR_PATH $SB_ARGS
I don't know why it works after using double quotes for the "-jar" option only, I think it's something to do with UNIX systems.

Get exit code from a java application in batch file

I'm currently making an effort to create test cases for one of our java applications.
In my code, my java application calls a batch file, which in turn starts a separate java process, that returns an error code that I need to consume from the calling java application.
I'm doing the following to invoke my batch file:
Process process = runTime.exec(new String[]{"cmd.exe","/c",scriptPath});
exitValue = process.waitFor();
The batch file is as follows:
#echo off
cd %~dp0
java -cp frames.FrameDriver
SET exitcode=%errorlevel%
exit /B %exitcode%
Now with the above code and batch file, my JUnit framework just hangs on this particular test case, as if it's waiting for it to end. Now when JUnit is hanging on the test case, going to the Task Manager, and ending the java.exe process would allow the JUnit framework to continue with the other cases.
Running the .bat file by double clicking it runs the Java application normally.
Adding the START batch command before the java command in my batch file seems to fix the hanging problem, but I can't seem to get the correct exit code from my Java application as it's always 0. (The Java application exits with an error code using System.exit(INTEGER_VALUE)). I'm assuming that the %errorlevel% value is being overwritten by the "start" command's own exit value.
Can anyone please tell me how to solve this problem?
Thanks.
P.S: If it makes any difference, I'm using JDK 5 and Netbeans 5.5.1.
Don't use the /B on your exit. Here is how I would do a script:
#ECHO off
ECHO Running %~nx0 in %~dp0
CALL :myfunction World
java.exe -cp frames.FrameDriver
IF NOT ERRORLEVEL 0 (
SET exitcode=1
) ELSE (
SET exitcode=0
)
GOTO :END
:myfunction
ECHO Hello %~1
EXIT /B 0
:END
EXIT %exitcode%
NOTE: Also, you can execute java program in 3 different ways:
java.exe -cp frames.FrameDriver
CALL java.exe -cp frames.FrameDriver
cmd.exe /c java.exe -cp frames.FrameDriver
This is very critical since, your Java command may exit with a exit code and in order to pass the exit code correctly to the ERRORLEVEL var, you need to use the correct method above, which I am unsure about.

Categories