Reading STDIN in Java that was started from a batch file












1















I have a console Java application, that depends on a lot of jar package. Therefore, I put all these packages in a batch file together. The only problem is, that if I start the Java app from the batch file I can't read from STDIN. I mean, I would like to process some user input, that was typed on the console, but nothing happens in the app.



To start the app from within the batch, I use this code (based upon this thread):



setlocal
for /F "tokens=*" %%a in ('more') do (
#%%a java -classpath ...
)


If I just invoke the Java app from the batch without the FOR loop, I also can't access to STDIN.



If I start the app directly from cmd, I can read from STDIN without any problems.



This is the code I am using to read from STDIN.



I really would like to use a batch file, because the command-line for the Java app is very long.



Is there a trick to redirect the STDIN to Java, or at least to a file ?










share|improve this question

























  • Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

    – eckes
    Nov 27 '18 at 3:41











  • Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

    – Perdi Estaquel
    Nov 27 '18 at 4:51











  • The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

    – sst
    Nov 27 '18 at 5:32











  • I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

    – awgold90
    Nov 27 '18 at 9:12











  • My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

    – awgold90
    Nov 28 '18 at 9:00


















1















I have a console Java application, that depends on a lot of jar package. Therefore, I put all these packages in a batch file together. The only problem is, that if I start the Java app from the batch file I can't read from STDIN. I mean, I would like to process some user input, that was typed on the console, but nothing happens in the app.



To start the app from within the batch, I use this code (based upon this thread):



setlocal
for /F "tokens=*" %%a in ('more') do (
#%%a java -classpath ...
)


If I just invoke the Java app from the batch without the FOR loop, I also can't access to STDIN.



If I start the app directly from cmd, I can read from STDIN without any problems.



This is the code I am using to read from STDIN.



I really would like to use a batch file, because the command-line for the Java app is very long.



Is there a trick to redirect the STDIN to Java, or at least to a file ?










share|improve this question

























  • Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

    – eckes
    Nov 27 '18 at 3:41











  • Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

    – Perdi Estaquel
    Nov 27 '18 at 4:51











  • The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

    – sst
    Nov 27 '18 at 5:32











  • I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

    – awgold90
    Nov 27 '18 at 9:12











  • My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

    – awgold90
    Nov 28 '18 at 9:00
















1












1








1








I have a console Java application, that depends on a lot of jar package. Therefore, I put all these packages in a batch file together. The only problem is, that if I start the Java app from the batch file I can't read from STDIN. I mean, I would like to process some user input, that was typed on the console, but nothing happens in the app.



To start the app from within the batch, I use this code (based upon this thread):



setlocal
for /F "tokens=*" %%a in ('more') do (
#%%a java -classpath ...
)


If I just invoke the Java app from the batch without the FOR loop, I also can't access to STDIN.



If I start the app directly from cmd, I can read from STDIN without any problems.



This is the code I am using to read from STDIN.



I really would like to use a batch file, because the command-line for the Java app is very long.



Is there a trick to redirect the STDIN to Java, or at least to a file ?










share|improve this question
















I have a console Java application, that depends on a lot of jar package. Therefore, I put all these packages in a batch file together. The only problem is, that if I start the Java app from the batch file I can't read from STDIN. I mean, I would like to process some user input, that was typed on the console, but nothing happens in the app.



To start the app from within the batch, I use this code (based upon this thread):



setlocal
for /F "tokens=*" %%a in ('more') do (
#%%a java -classpath ...
)


If I just invoke the Java app from the batch without the FOR loop, I also can't access to STDIN.



If I start the app directly from cmd, I can read from STDIN without any problems.



This is the code I am using to read from STDIN.



I really would like to use a batch file, because the command-line for the Java app is very long.



Is there a trick to redirect the STDIN to Java, or at least to a file ?







java batch-file stdin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 12:35









double-beep

2,39641026




2,39641026










asked Nov 27 '18 at 0:14









awgold90awgold90

316




316













  • Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

    – eckes
    Nov 27 '18 at 3:41











  • Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

    – Perdi Estaquel
    Nov 27 '18 at 4:51











  • The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

    – sst
    Nov 27 '18 at 5:32











  • I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

    – awgold90
    Nov 27 '18 at 9:12











  • My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

    – awgold90
    Nov 28 '18 at 9:00





















  • Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

    – eckes
    Nov 27 '18 at 3:41











  • Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

    – Perdi Estaquel
    Nov 27 '18 at 4:51











  • The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

    – sst
    Nov 27 '18 at 5:32











  • I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

    – awgold90
    Nov 27 '18 at 9:12











  • My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

    – awgold90
    Nov 28 '18 at 9:00



















Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

– eckes
Nov 27 '18 at 3:41





Why do you use more (like this)? It captures stdin and does not pass it to any subcommand (java in your case). It might be better to use java -cp ... | more instead, but even then the resulting batch file is hard to use for scripting. (and the purpose of the tokens parsing is not clear to me at all)

– eckes
Nov 27 '18 at 3:41













Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

– Perdi Estaquel
Nov 27 '18 at 4:51





Don't have the answer but this is not a java question, it's a cmd batch question. You want to redirect cmd batch stdin to the process called from within the batch.

– Perdi Estaquel
Nov 27 '18 at 4:51













The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

– sst
Nov 27 '18 at 5:32





The FOR loop you have used doesn't make any sense,The more command captures the stdin and blocks the execution of the batch code until it terminates by pressing CTRL-Z (or CTRL-C) then you have #%%a java -classpath ... it would be translated to #WhatEverHaveBeenCapturedByMore java -classpath .... What are you trying to achieve with that? Did you get that code from someone or wrote it yourself? "If i start the app directly from cmd.exe, i can read from STDIN without any problems." Just do the same in the batch file as you did in CMD prompt.

– sst
Nov 27 '18 at 5:32













I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

– awgold90
Nov 27 '18 at 9:12





I added the thread, i used to build the batch command. My first try was to invoke the Java app from the batch without the <code>FOR</code> loop, but it didn't work. Then i thought maybe it have to be forced, so i used <code>more</code>. I would like to access to STDIN from the Java app, that is invoked from the batch, but it seems, the batch is blocking Java from doing that.

– awgold90
Nov 27 '18 at 9:12













My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

– awgold90
Nov 28 '18 at 9:00







My next try was to use the command start to execute the Java application in an other window. This worked well. But after this, i realized that invoking the Java app directly from a batch also worked. I have no imagination, what could be the problem.

– awgold90
Nov 28 '18 at 9:00














0






active

oldest

votes











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%2f53490981%2freading-stdin-in-java-that-was-started-from-a-batch-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53490981%2freading-stdin-in-java-that-was-started-from-a-batch-file%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)