-1

How can we pass a batch variable as parameter to a Java class main method using command line? I want to pass the contents of a text file as an argument to a Java class using command line. for eg : Java -jar TestJar.jar %BATCH_VAR%

I have tried the below code and it doesnt seem to work :

echo "starting"

echo off

set keyvalue=^&Type TestDoc.txt

echo %keyvalue%  /// This is printing the right value.

pause

java -jar ErrorUpdate.jar "%keyvalue%" //// This does not pass anything to the Java class :(

pause
M.A.K. Ripon
  • 2,070
  • 3
  • 29
  • 47

1 Answers1

2

I suspect that you may have a misconception about what value is stored in your variable. So let me clarify exactly what is going on in your batch file.

set keyvalue=^&Type TestDoc.txt
echo %keyvalue%  /// This is printing the right value.

That is not printing the right value. What you have done is assigned the value &Type TestDoc.txt to the variable. When you then type echo %keyvalue%, this line gets expanded to the following line:

echo &Type TestDoc.txt

This is actually two separate commands. The first, echo, simply queries if the echo setting is currently on or off. The second command, Type TestDoc.txt, is then executed.

At no point does the variable keyvalue ever contain the contents of the file.

So when you have this line in your batch:

java -jar ErrorUpdate.jar "%keyvalue%"

It gets expanded to:

java -jar ErrorUpdate.jar "&Type TestDoc.txt"

This time, the & is enclosed in quotes, so it does not act as a statement separator, but instead is passed to your java class's main method. Your main method just sees the string "&Type TestDoc.txt" passed in as args[0].

If you want the java application to receive the full contents of the file, you need to make your java application read the contents itself.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42