This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Ashwin Sridhar wrote:I need to capture the value once I get past this block. My main aim is to capture the return value from the java method.
Return value or output? The two are not the same.
Return values can be generated by System.exit(), but to my mind they're pretty clunky. They also generally involve dispatch code, viz:
my program returned 0, so I do this
my program returned 1, so I do this
my program returned 43, so I do this
... not really very Object-Oriented.
Output, on the other hand, can be piped directly to another process.
The problem is, as yet, we have no idea what you're trying to do; just what you've already done (which plainly isn't working), so it's very difficult to advise you on how to proceed.
Your original question really was a bit unclear, and besides, it has nothing to do with Java. The equal sign disappears while CMD is processing the FOR command (I tried that myself), and frankly I don't know why. The following BAT file
gives me this output:
(I've modified your FOR command to not consider equal sign as a delimiter. Apart from the fact it confuses your question a lot , it would break my test, since it doesn't contain your Java processing.)
So the equal sign gets replaced by a space by the FOR command. But this happens only if you run the FOR to parse the output of a program, and I'd say this might be a bug in CMD.EXE.
Possible workaround would be to execute the command outside of the FOR, redirect the output to a temp file, and then parse that file, as I've done.
Much better would be to move this processing to Java too. Manipulating special characters in BAT files is fiendishly difficult and prone to errors. Don't do it.
Ashwin Sridhar wrote:I am sorry for not helping you understand what my problem is. I am trying to pass the output from java program to batch file.
I pass the input from batch to java program and want to store the String passed from java in a variable in batch.
I write to the outputstream in Java program using System.out.println and trying to capture the same here.
Right, so you're dealing with program output. And I'm afraid that this is where my expertise ends. I could easily tell you how to do it in Unix, but not in Windows, which is why I say that you might be better off checking out an MSDN forum.
What I would say is that it's probably best to have your Java program process ALL arguments in one go, rather than calling it once for each string.
It looks like some sort of decryption program, and personally, I'd have it return each encrypted argument to the output, one decrypted line per input argument, and then pipe that to the next process.
Also, in Unix, programs written to interact with other processes are generally written to accept input either via arguments OR via sysin.
However, all this faffing about with scripts seems superfluous to me, and over-engineered.
But then again, I still don't know exactly what you're trying to do.
Ashwin Sridhar wrote:I got over this by using double-quotes instead of single quotes. So my command looks like below,
But I don't find the output set in the variable decryptedtest.
If you put the string in double quotes, it is not executed, but parsed as it is - therefore you cannot execute a program this way. So this is not a workaround.
I have run my jar separately for same input and it works fine.
Yes, the problem is clearly caused by the BAT file and nothing else.
Follow Winston and my advice and move the processing to your Java program (assuming it is your program, if it isn't, write one and call the other Java from it). This hassle with the BAT file is really not worth it. Even if you happen to solve the equal sign someday, you might discover later that other characters (such as ^ or &) have broken your processing again. BAT files really were not designed for this.