Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Initiate System.console() when the executable jar calling in windows batch script  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am facing issue in System.console() when i call the executable jar file inside windows batch script. Now what is my use case i am writing windows batch script for my business case and i need to get password of database from the user.
So to mask the password entered by user i try to create a Java executable and tried calling inside the windows batch file. The problem is if i run my executable directly in command prompt this works fine and the password is masked.
But if i call the same jar inside batch script its not able to get the System.console() i understand if its running inside batch script this should also be in console mode or is it different .
Below is my sample code of java class and the batch script . Can some one help me on this .

Java code :

public class MyPasstesting {

public void passwordExample() {

Console console = System.console();
if (console == null) {
System.out.println("Console is empth ");

char passwordArray[] = console.readPassword("Enter your secret password: ");

console.printf(new String(passwordArray));


public static void main(String[] args) {
new MyPasstesting().passwordExample();


Exported the project in to Runnable jar 'test.jar'

Batch script :


@CALL :getPassword pwd
@echo "What i got SCN :" %pwd%

@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%J in ('java -jar test.jar') do (
set "spwd=!spwd! %%J"
endlocal & set spwd=%spwd%
@SET %1=%spwd%
@GOTO :eof
Posts: 58367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. What will happen is that System.console() will consistently return null, and you cannot use that Console.

Shame: the Console class looked such a nice thing when it first came out, but you can hardly ever use it if you run a .jar or if you use an IDE.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!