The following is a snippet from a batch file where I try to run myProgram.jar
set /p _count="enter count: "
set /p _toc="enter TOC"
choice /N /C:TO /M "Select Type(T/O): "
if errorlevel ==1 set _type=T
if errorlevel ==2 set _type=O
choice /N /C:SF /M "Select status(S/F): "
if errorlevel ==1 set _status=T
if errorlevel ==2 set _status=O
java -jar dir/myProgram.jar %_count% %_type% %_status% %_toc%
The batch file executes the java -jar
command with four arguments namely _count
, _type
, _status
and _toc
.
I only get the values of _count
and _toc
in java.
The problem is the remaining two parameters are getting passed as null
values and as a result I am facing NullPointerException
everytime I run the program.
From CHOICE/?
:
When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
That means you use the following syntax:
If ErrorLevel 2 Set "_type=O"
If ErrorLevel 1 Set "_type=T"
An often used alternative, if you prefer it, is to use %ERRORLEVEL%
instead:
If "%ErrorLevel%"=="1" Set "_status=T"
If "%ErrorLevel%"=="2" Set "_status=O"
Your error can't be reproduced and I'm quite sure it leads to a simple typographical error or in your Java App, which you didn't show to us.
I did the following to reproduce your behavior:
args
to the console: import java.util.Arrays;
class App {
public static void main(String[] args) {
Arrays.stream(args).forEach(System.out::println);
}
}
jdk1.8.0_91
on Windows 10. javac .\App.java
jar cvfe App.jar App *.class
set /p _count="enter count: "
set /p _toc="enter TOC"
choice /N /C:TO /M "Select Type(T/O): "
if errorlevel ==1 set _type=T
if errorlevel ==2 set _type=O
choice /N /C:SF /M "Select status(S/F): "
if errorlevel ==1 set _status=T
if errorlevel ==2 set _status=O
java -jar App.jar %_count% %_type% %_status% %_toc%
With the inputs: 5, 6, T, F
it gave the following output:
5
T
O
6
That means - IMHO - your error lays in your java code obtaining args
, the NullPointerException
is thrown by System.Console()
and not args[n]
, or your posted batch code isn't exactly how it is executed on your computer.
To sum it up: If your batch calls the command line with 4 parameters (like your example code does), args
has 4 parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.