简体   繁体   中英

Java IO with a Jar file using command line

I want to be able to open a jar file and have it open a console that I can interact with it from. I have tried opening the jar with java.exe instead of javaw.exe , but the window that appears closes almost instantly when I try to run the program. What am I doing wrong?

  1. you run java.exe not from the console.
  2. the program jar cannot be run, has errors, or exited immediately.
  3. to run java.exe on Windows OS requires to create a surrogate console to run a console program.
  4. javaw.exe doesn't open a console, so you can't see the output.

Your program is closed because it is not waiting for input data.

See example:

public static void main(String[] args) throws Exception {  
    System.out.println("Hello! I'm waiting for input...");
    args = new Scanner(System.in).nextLine().split(" ");            
    System.out.println("I received your data: \r\n\r");
    for(int i=0; i<args.length; i++) System.out.print(args[i] + " ");
    System.out.println("\r\n\rAfter 10 second I'll close");
    Thread.sleep(10 * 1000);
    // Here program did not waiting for input and was closed automatically;
}   

What shown in console:

C:\Users\user>java -jar "C:\myprogram.jar"
Hello! I'm waiting for input...
bla blabla bla bla // I did enter it to console
I received your data:

bla blabla bla bla
After 10 second I'll close

After 10 seconds program was closed, because not waiting for input data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM