简体   繁体   English

使用命令行将Java IO与Jar文件配合使用

[英]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. 我希望能够打开一个jar文件,并使它打开一个可以与之交互的控制台。 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. 我尝试用java.exe而不是javaw.exe打开jar,但是当我尝试运行程序时,出现的窗口几乎立即关闭。 What am I doing wrong? 我究竟做错了什么?

  1. you run java.exe not from the console. 您不是从控制台运行java.exe。
  2. the program jar cannot be run, has errors, or exited immediately. 程序jar无法运行,出现错误或立即退出。
  3. to run java.exe on Windows OS requires to create a surrogate console to run a console program. 在Windows OS上运行java.exe要求创建代理控制台以运行控制台程序。
  4. javaw.exe doesn't open a console, so you can't see the output. javaw.exe不会打开控制台,因此您看不到输出。

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. 10秒后,由于不等待输入数据,程序已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java命令行Jar文件 - Java Command Line Jar File 使用命令行控制Java jar的输出文件 - Controlling output file of a java jar using command line Maven:尝试使用Intellij或通过jar使用Java命令从资源中打开文件时,java.io.FileNotFoundException - maven: java.io.FileNotFoundException when trying to open file from resources with Intellij or using java command with jar 从命令行调试Java jar文件? - debug java jar file from command line? Java jar文件的命令行中的选项 - Options in command line of Java jar file 使用导入的jar的Java命令行 - Java command line using an imported jar 如何使用以下命令行在类 UNIX 中执行 jar 文件:java -jar mysolution.jar &lt; inputfile.txt - How can I execute jar file in UNIX-like using this command line: java -jar mysolution.jar < inputfile.txt 无法使用Java程序执行jar文件。 我需要将文件路径作为命令行参数传递给jar文件 - unable to execute the jar file using java program. i need to pass the file path as a command line argument to the jar file 使用第三方jar在命令行中执行jar文件 - execute jar file in command line using third party jar 找不到属性文件:java.io.FileNotFoundException使用命令行运行Java程序 - Property file is not found :java.io.FileNotFoundException run java program using command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM