• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
karthikeyan Rajamanickam
Greenhorn
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 :
import java.io.Console;

public class MyPasstesting {

public void passwordExample() {

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

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 :

@ECHO OFF

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

:getPassword
@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
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • 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!