Executable Jar not Displaying Output - java

I have an executable jar that I know is executing, because I put two distinct beeps at the beginning of the code that I hear whenever I double click on it or when I run it through the cli. Even when I run it through the command line however, it does not display output nor prompt for input when I use System.out/System.in respectively. Everything is functional when I run it through eclipse.
How do I get the .jar to output/input to the same command line I executed it in?

Assuming java is in your PATH, you should lauch your program as follows :
java -jar myProgram.jar
If you want to run it without opening the command line, you could create a .cmd file in the same directory with this content :
#echo off
java -jar myProgram.jar
pause
then, double-click on the .cmd file.

Related

Jar file execution

Suppose I made a program to take two input and display the sum.
It runs in netbeans but when I build out the jar file , and
double clicked the jar file , nothing happens.
How can I make these program run?
You must run this from command line, and then execute java -jar {PATH_TO_JAR}
You can use the command-line :
java -jar <yourjar> <yourMainClass>
You can also add the main class in a manifest file into the jar. Doing so, you don't need to specify anymore on command line
Jar files needs to run in JVM - Java Virtual machine, to run your file use:
"java -jar" and your jar.
By doing so, you tell Java to run your jar in jvm which means it now can run.

Invoke jar file via bat and get the output to same console

I have test.jar which just contain a single class and it has a main method. That just prints out some string to std out. I need to run this jar using .bat file. So I used
#echo off
start java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
in my script.
When I run the bat file the output does not print to the same console. Instead it is opened in another window.
( What I understand is I should put some 'echo variable' where the output of the java program should be assigned to 'variable'. Or There might be some other ways ).
What should I do?
Thank You !
The start is what is causing the new window to pop up and the output is going there. Change your bat file to say this instead:
#echo off
java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
You can also remove the #echo off if you want. If you remove that line, the windows shell will print out the lines it's executing in your bat file. I'd personally leave #echo off there except when you're trying to debug the bat file.

How to call for the system console in Java?

I made a console-based Java application but every time I try to start the .jar file by clicking on it the program seems to be running but there is no console displayed. Is there specific code I must write in order to call for the system console?
Can you start the console first , change directory to where your jar file is and then run java -jar yiurjarfilename ?
The OS takes care of displaying console output. There is no code that you can write within Java to display or hide the "console" (because within Java, there's only standard output & error streams that you write to).
Windows usually leaves the console open after your program exits, but there might be a setting within the Java Runtime Environment that configures that behavior.
gcivil is right, you can see the results in console only if you start from console, if you are in windows you can open the command line Super + R and type cmd, then press enter (Super is the one with the Windows icon)
there you can type : java -jar "absolute path to your file" (don't forget the quotes)
another way is create a .bat file next to the .jar one, the bat file should contain
java -jar filename.jar
you don't need the quotes nor absolute path since it is next to the .bat file now you can double click that instead of the .jar
Once the app is terminated it will close the console, if you need to see what is next you have to add pause
java -jar filename.jar
pause

Error Running batch file from Windows desktop to execute a Java program in eclipse workspace

I have a Java standalone project in Eclipse with about 10 packages. I have a main method(in eclipse) that when executed from Eclipse works fine.
I have written a batch file to run it from the desktop. I just click the batch file and hope to run the program.
My code for the batch file is as follows.
RunExecuteMyProg.bat
echo Output of the Program
echo ---------------------
java C:\eclipse_workspace\eclipse\myprogram\MainProgram\ExecuteMainProgram
echo "Program Executed"
This program when run in Eclipse, usually takes between 1 -4 min depending on a number of factors. But when I click the .bat file, it opens for a fraction of a second and closes. Java is on my classpath. At command prompt when I try to compile, I get compile errors saying that some class is not found. However on eclipse it just runs fine. Log files need to get created when this program runs, but nothing happens from batch file.
PS: The class files are created in the same folder as the source files.
You will know the problem if you open a command prompt and enter that command you have there:
java C:\eclipse_workspace\eclipse\myprogram\MainProgram\ExecuteMainProgram
It could be that you don't have java in the path or your program is written so that it has to have its current working directory where the program is located, etc etc.
Or any number of things. Get the output from executing that command manually in a command prompt.

Starting a blank console

IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat

Categories

Resources