简体   繁体   中英

How to see output on command prompt by running a jar/exe file?

I have created a jar which reads values from excel, does some manipulations and write the results back to excel.

When I run the jar, it works perfectly. I have also created an exe file for the jar and it works very well too.

However, while the values are being calculated for data on each row of the excel, I want the result to be displayed on command prompt too, for which i have syso statements.

I am able to achieve this by creating a batch file and running the jar through .bat file. But when i run the exe file through a bat file, I'm not able to see the calculations on cmd.

Is there any way where I can see the calculated values in runtime through syso statements, on command prompt, just by double clicking on exe file?

PS: I do not want to run the jar through java -jar command on cmd. I want calculation results to be displayed on cmd if i just double click the jar/exe.

Noted down steps how to create a Runnable Jar file from eclipse tool. Oncle you done with below steps, you will get a runnable jar file. If you double click on the jar file, it will start executing your jar file through a command prompt.

  • Create a batch file, and placed under any path in your system. In the example "C:\\Users\\username\\Desktop\\test\\my_script.bat"

      @ECHO OFF set java_path=D:\\Java\\jdk1.6.0_41 cd /d D:\\rootFolder\\extractData %java_path%\\bin\\java -jar yourJARfilename.jar cmd /K EXIT /B 0 
    • Create a java project.
    • Create a java a class for executing batch file. Specify your batch file path as coded below.

       package com.cmd.test; import java.io.IOException; public class AutoExeJAR { public static void main(String[] args) { try{ Runtime r=Runtime.getRuntime(); r.exec("cmd.exe /c start call C:\\\\Users\\\\username\\\\Desktop\\\\\\test\\\\my_script"); }catch( IOException ex ){ //Validate the case the file can't be accesed (not enought permissions) }}} 
    • Right click on the above java project --> Export --> Runnable Jar -> (Select the above class file in Launch configuration dropdown in the popup window) -> Give the path for exporting as runnable jar file. NOTE : Selecting your class file name here is specifying invoking point soon after the jar file is double clicked.

    • Double click on that exported runnable jar file which executes the batch file automatically.
    • Please refer the link as well.

There is an option on Launch4J to choose if the output should be displayed on console during runtime! I just selected it and the results are shown on console while running the exe file.

Thank you all for the suggestions.

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