Redirect java -version to file or variable












6















Maybe it is a silly question, but I'm trying to redirect the exit of "java -version" command to a file or variable but it doesn't work.



Server = Linux CentOS 6



My code in shell script



java -version >> test.txt


Also I'm trying to assign it to a variable:



JAVA_CHECK=`java -version`


Even running those command from command-line it still not working.



when I say it doesnt work, I mean that the exit of the command is being showed in my screen instead to redirect it to a file or wherever



...










share|improve this question























  • its a good question - this is happening on windows too

    – user1428716
    Jan 30 '14 at 10:31


















6















Maybe it is a silly question, but I'm trying to redirect the exit of "java -version" command to a file or variable but it doesn't work.



Server = Linux CentOS 6



My code in shell script



java -version >> test.txt


Also I'm trying to assign it to a variable:



JAVA_CHECK=`java -version`


Even running those command from command-line it still not working.



when I say it doesnt work, I mean that the exit of the command is being showed in my screen instead to redirect it to a file or wherever



...










share|improve this question























  • its a good question - this is happening on windows too

    – user1428716
    Jan 30 '14 at 10:31
















6












6








6








Maybe it is a silly question, but I'm trying to redirect the exit of "java -version" command to a file or variable but it doesn't work.



Server = Linux CentOS 6



My code in shell script



java -version >> test.txt


Also I'm trying to assign it to a variable:



JAVA_CHECK=`java -version`


Even running those command from command-line it still not working.



when I say it doesnt work, I mean that the exit of the command is being showed in my screen instead to redirect it to a file or wherever



...










share|improve this question














Maybe it is a silly question, but I'm trying to redirect the exit of "java -version" command to a file or variable but it doesn't work.



Server = Linux CentOS 6



My code in shell script



java -version >> test.txt


Also I'm trying to assign it to a variable:



JAVA_CHECK=`java -version`


Even running those command from command-line it still not working.



when I say it doesnt work, I mean that the exit of the command is being showed in my screen instead to redirect it to a file or wherever



...







java linux variables redirect command






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 30 '14 at 10:25









SallyerikSallyerik

175616




175616













  • its a good question - this is happening on windows too

    – user1428716
    Jan 30 '14 at 10:31





















  • its a good question - this is happening on windows too

    – user1428716
    Jan 30 '14 at 10:31



















its a good question - this is happening on windows too

– user1428716
Jan 30 '14 at 10:31







its a good question - this is happening on windows too

– user1428716
Jan 30 '14 at 10:31














1 Answer
1






active

oldest

votes


















10














java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:



java -version 2> test.txt
# cat test.txt
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]


Or you can redirect stderr to stdout:



java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]





share|improve this answer


























  • Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

    – Sallyerik
    Jan 30 '14 at 10:56











  • Probably you forgot the dash before the version: java version will try to run a class named version

    – Nigel Tufnel
    Jan 30 '14 at 11:00













  • Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

    – Sallyerik
    Jan 30 '14 at 11:06











  • Fascinating! Which shell are you using? Works for me on ubuntu/bash.

    – Nigel Tufnel
    Jan 30 '14 at 11:12













  • Well yes it's. I'm using CentOS/bash.

    – Sallyerik
    Jan 31 '14 at 9:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21453774%2fredirect-java-version-to-file-or-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:



java -version 2> test.txt
# cat test.txt
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]


Or you can redirect stderr to stdout:



java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]





share|improve this answer


























  • Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

    – Sallyerik
    Jan 30 '14 at 10:56











  • Probably you forgot the dash before the version: java version will try to run a class named version

    – Nigel Tufnel
    Jan 30 '14 at 11:00













  • Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

    – Sallyerik
    Jan 30 '14 at 11:06











  • Fascinating! Which shell are you using? Works for me on ubuntu/bash.

    – Nigel Tufnel
    Jan 30 '14 at 11:12













  • Well yes it's. I'm using CentOS/bash.

    – Sallyerik
    Jan 31 '14 at 9:26
















10














java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:



java -version 2> test.txt
# cat test.txt
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]


Or you can redirect stderr to stdout:



java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]





share|improve this answer


























  • Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

    – Sallyerik
    Jan 30 '14 at 10:56











  • Probably you forgot the dash before the version: java version will try to run a class named version

    – Nigel Tufnel
    Jan 30 '14 at 11:00













  • Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

    – Sallyerik
    Jan 30 '14 at 11:06











  • Fascinating! Which shell are you using? Works for me on ubuntu/bash.

    – Nigel Tufnel
    Jan 30 '14 at 11:12













  • Well yes it's. I'm using CentOS/bash.

    – Sallyerik
    Jan 31 '14 at 9:26














10












10








10







java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:



java -version 2> test.txt
# cat test.txt
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]


Or you can redirect stderr to stdout:



java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]





share|improve this answer















java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:



java -version 2> test.txt
# cat test.txt
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]


Or you can redirect stderr to stdout:



java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 11:14









tripleee

93.6k13130185




93.6k13130185










answered Jan 30 '14 at 10:27









Nigel TufnelNigel Tufnel

8,14812628




8,14812628













  • Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

    – Sallyerik
    Jan 30 '14 at 10:56











  • Probably you forgot the dash before the version: java version will try to run a class named version

    – Nigel Tufnel
    Jan 30 '14 at 11:00













  • Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

    – Sallyerik
    Jan 30 '14 at 11:06











  • Fascinating! Which shell are you using? Works for me on ubuntu/bash.

    – Nigel Tufnel
    Jan 30 '14 at 11:12













  • Well yes it's. I'm using CentOS/bash.

    – Sallyerik
    Jan 31 '14 at 9:26



















  • Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

    – Sallyerik
    Jan 30 '14 at 10:56











  • Probably you forgot the dash before the version: java version will try to run a class named version

    – Nigel Tufnel
    Jan 30 '14 at 11:00













  • Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

    – Sallyerik
    Jan 30 '14 at 11:06











  • Fascinating! Which shell are you using? Works for me on ubuntu/bash.

    – Nigel Tufnel
    Jan 30 '14 at 11:12













  • Well yes it's. I'm using CentOS/bash.

    – Sallyerik
    Jan 31 '14 at 9:26

















Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

– Sallyerik
Jan 30 '14 at 10:56





Great it works!!. But I dont undestand why Java writes to sterr instead to stdout. And other weird issue happed when I try to assign the variable command within my shell scrip, it's works, I mean it's doing well the redirection but it's feeding my variale with "Error: Could not find or load main class version"

– Sallyerik
Jan 30 '14 at 10:56













Probably you forgot the dash before the version: java version will try to run a class named version

– Nigel Tufnel
Jan 30 '14 at 11:00







Probably you forgot the dash before the version: java version will try to run a class named version

– Nigel Tufnel
Jan 30 '14 at 11:00















Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

– Sallyerik
Jan 30 '14 at 11:06





Unfortunately not, the dash still there, I've copied and pasted the comand. It's prety corious because 2> test.txt works but 2>&1 shows the error.

– Sallyerik
Jan 30 '14 at 11:06













Fascinating! Which shell are you using? Works for me on ubuntu/bash.

– Nigel Tufnel
Jan 30 '14 at 11:12







Fascinating! Which shell are you using? Works for me on ubuntu/bash.

– Nigel Tufnel
Jan 30 '14 at 11:12















Well yes it's. I'm using CentOS/bash.

– Sallyerik
Jan 31 '14 at 9:26





Well yes it's. I'm using CentOS/bash.

– Sallyerik
Jan 31 '14 at 9:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21453774%2fredirect-java-version-to-file-or-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly