• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic