• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Is there anyway I can get the result of java program out put to bash script variable ?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is there anyway I can get the result of java program out put to bash script variable ? Suppose I have jar with main() and print just string,what I want to do is I want to take that string to shell script variable.
 
author
Posts: 23901
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bash (actually, *all* of the shells) allows you to set a script variable to the output of a program -- via usage of the single back quote.

So, you program can simply print the result to standard output. And exit, of course, as Bash needs to get control back to finish the assignment...

Henry
 
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't do this from Java. Use your shell's capabilities. In Bash, you can try this:
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Bash (actually, *all* of the shells) allows you to set a script variable to the output of a program -- via usage of the single back quote.

So, you program can simply print the result to standard output. And exit, of course, as Bash needs to get control back to finish the assignment...

Henry

Thanks for the reply Henry,Could please give me the example.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Don't do this from Java. Use your shell's capabilities. In Bash, you can try this:

Thanks for the reply Stephan,But its not working in my scenario.
 
Henry Wong
author
Posts: 23901
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Both the usage of backquotes, and the usage of $() should work.  If it doesn't, or seems to not work, you need to provide more details.  This is a debugging situation... and we can't help you debug with no visibility.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by calling just your java command. Show us the output of calling that.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Both the usage of backquotes, and the usage of $() should work.  If it doesn't, or seems to not work, you need to provide more details.  This is a debugging situation... and we can't help you debug with no visibility.

Henry



Here is the java code



Here is the .bat file


 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your application doesn't print anything to standard output. You're using the error stream.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Your application doesn't print anything to standard output. You're using the error stream.

Same result for output stream aswell
 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, show us the output of just calling your java command.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Again, show us the output of just calling your java command.



Sorry,I could not able to paste the image here
 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't post an image. Copy the output from your terminal.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You shouldn't post an image. Copy the output from your terminal.



 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is MY_VAR visible in the output when you run the following?
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Is MY_VAR visible in the output when you run the following?



No,

 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not the command I posted. set is a separate command on a separate line.
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's not the command I posted. set is a separate command on a separate line.



sorry for that,

Here is the out put first command

E:\WS\26-05-2017\test\target>MY_VAR=$(java -cp test-0.0.1-SNAPSHOT.jar com.test.
test.App)
'MY_VAR' is not recognized as an internal or external command,
operable program or batch file.

and for set there is no variable with the name MY_VAR
 
Henry Wong
author
Posts: 23901
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you running the bash command shell? It looks like the windows shell to me.

Henry
 
Sheriff
Posts: 22257
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is Windows, and that has a completely different syntax (which I don't really know since I hate writing batch/cmd scripts - just give me Bash instead!).
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Are you running the bash command shell? It looks like the windows shell to me.

Henry

Henry ,You are correct that is windows shell(cmd) and I need to write the batch file(.bat) for the same requirements mentioned above.Could you please help me on this?
 
Stephan van Hulst
Saloon Keeper
Posts: 13072
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution is the same as for Bash. Just google how to set environment variables from CMD.
 
Henry Wong
author
Posts: 23901
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi kapa wrote:Henry ,You are correct that is windows shell(cmd) and I need to write the batch file(.bat) for the same requirements mentioned above.Could you please help me on this?


Yeah, you kinda should have mentioned this earlier. Or at the very least, don't send us on the wrong direction. Kinda wasted lots of time here.

Stephan van Hulst wrote:The solution is the same as for Bash. Just google how to set environment variables from CMD.


I am not completely sure of this. It has been many years since I worked with Windows batch files, but I don't recall that the equivalent was at all similar.

If I remember correctly, you have to use the command option for the FOR loop, to loop through the output, which is then set to the windows variable...

[UPDATE] Regardless, Stephan is correct. Google is the way to go... https://stackoverflow.com/questions/6067199/equivalent-of-unix-backticks-in-dos

Henry
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic