aspose file tools*
The moose likes Java in General and the fly likes How to capture return value of a method in Batch file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to capture return value of a method in Batch file" Watch "How to capture return value of a method in Batch file" New topic
Author

How to capture return value of a method in Batch file

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi All,

I am trying to call my java program and capture the return value and assign to a variable Below is the code,



My test_str contains the value "%ENC%_j2e9+CnGH54\=". But from console, I find that the "=" sign at the end is removed and value passed to java program is "%ENC%_j2e9+CnGH54\".

Could someone have a idea why this happens?


Ashwin Sridhar
SCJP | SCWCD | OCA
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ashwin Sridhar wrote:Could someone have a idea why this happens?

Not really. This looks like a .bat file, so wouldn't it be a better question for an MSDN forum?

Also, I'm not quite sure what it has to do with your subject line, which seems to be about using values returned from a Java program.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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.

Winston
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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.

Please let me know if you want further information.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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.

Winston
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

The equal sign disappears while CMD is processing the FOR command (I tried that myself), and frankly I don't know why


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.

I have run my jar separately for same input and it works fine.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Already my processing is in my jar. I need the output of the process in order to call another process.

So ill try some other means of making it happen.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

You can run that other process from Java. This invaluable article tells you all you need to do.

In my opinion, it would be much more robust than batch files.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Thank you Martin for your suggestions.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Winston Gutkowski wrote: 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.


Good old bash (Bourne -again shell) is available fortunately for Windows too.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ivan Jozsef Balazs wrote:Good old bash (Bourne -again shell) is available fortunately for Windows too.

Really? Without having to install cygwin? That's an improvement.

Winston
 
Don't get me started about those stupid light bulbs.
 
subject: How to capture return value of a method in Batch file