How to call for the system console in Java? - 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

Related

How to keep the cmd open?

When we click on .jar files which do not contain any GUI the CMD Prompt runs the code and exits it immediately.
How to make it remain open when running a .jar by double clicking on it?
Is there any java code for it? Just like the pause command in batch files or any? I prefer only by java code but not an OS way? and also not by running command java - jar *.jar in cmd prompt.
I'd usually just open a command line first, as suggested in the other answer.
If you need a pure Java code solution though, just read from system in at the end of your code. The window will stay open while waiting for input.
Run the jar file from the command prompt
java -jar yourjarfilename.jar
Something along the lines read a character from std-in along with a print statement telling people to "press any key" should do the trick, provided you have access to the java code that is.
if not well, best option is to drop to CMD and run the command manually.

What command should I use to launch a jar file from the context menu using Windows registry?

I am currently trying to make a jar file launch from a right click on a folder's background.
I have found the context menu launch (can't remember where, but essentially add to HKEY_CLASSES_ROOT/Directory/Background/shell as new key and then add a command key in that with the action to do on click).
And the item location being %1 on this similar question on superuser: https://superuser.com/questions/444726/windows-how-to-add-batch-script-action-to-right-click-menu
But I am stuck on the code for the action of launching the program and passing it the directory.
My most recent attempt is this: java -jar "fullpath\program.jar" "%1"
java -jar to run it, path for the location, %1 to hopefully pass the directory path.
I've tried a bunch of variants of this too but can't get it working. Any ideas?
EDIT: What I did in the end.
I created a bat file and wrote in it:
start javaw "%~dp0/program.jar" "%cd%"
This runs the java file and sends it the users working directory. start javaw is used instead of just java to make the command line close after launching the java program. %~dp0 gets the bat file's directory.
The "%1" part will not work with directory background, but the program is started having that directory as working directory. (Windows 7 is giving an error if '"%1"' is in the command.)
Use
System.getProperty("user.dir");
to get it.
If you can't change the code, write a * wrapper* that gets this directory and calls the main method of the original class passing that directory as argument (or write a shell script to do that and invokes java with the argument)

Java - System.out.println() viewable from application?

I have a Java application that runs great :) While uploading files, it uses the standard output to show progress : "System.out.println(...);".
When I run it in Eclipse, well it works perfectly, but when I run the JAR file, I don't see any console/terminal showing up and printing what I print through "System.out.println(...),".
How can I open a new terminal when my application is launched (it is a Swing application)?
Basically I want to be able to run the Swing application and show information on the side in a terminal / console. Why? Don't worry about why I want to do this ;)
Thanks a lot!
Regards.
Open terminal and run application as java -cp yourjar.jar YouMain or java -jar yourjar.jar if you jar is runnable.
I believe that you do not see output because you are running your application using javaw - the special windows-only variation of JVM that does not have STDOUT at all. If you want to click your application and see output map *.jar file to be opened using java instead of javaw. Alternatively write bat file that runs your application. In this case you will see console.
Use java instead of javaw to launch your application. Double-clicking on a jar executes it with javaw. Instead, open a command line window and type
java -jar thePathOfTheJarFile.jar
If you want to have something double-clickable, then write a shell script containing this command, and double-click the shell script instead of the jar.

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

closing a command line window in Java

I have created a small Java application in which there is some code that executes a batch file. The execution of the batch file leads to the command line window to be opened and to display some output messages. I would like to know if there is some way in Java to call this command line window to be closed from within the program...Thanks!
the command window should close automatically when the batch file completes.
and to run a batch file in background/invisibly, check other questions
Start java by using javaw or javaw.exe.
java (java.exe) runs with an associated console window,
javaw (javaw.exe) is the same but without the console window.
see the documentation for the java command.
On Windows also use start to invoke another shell
start javaw ClassName
I'm not sure for Linux&Co. Try using an & after the command to run it in the background
javaw ClassName &
The other way, closing the window from a batch started by Java:
I don't believe that is possible directly from within Java. You can only close the batch file by itself.
Is hard to help without knowing what that batch file is doing. You may try using the start command on windows or the & in Linux to run the process in the background (start has an option to open the window minimized [/MIN] or in the background [/B]).
Or try some hack like using AutoHotKey or some system functionality (some WinAPI-DLL or equivalent in other systems).
As an addition to NimChimpsky's answer:
If you run a batch file in Windows, Windows will automatically open a command window for the batch file, in case the batch file wants to print output or prompt for input. This also applies if you launch the bat file from a Java process.
Unfortunately, Windows itself apparently provides no way to launch a batch file without such a window. To avoid the window, you will have to run the batch file via some helper program. There are several available; google for "run bat no window" to find some.
If you just want the window to go away after the batch file terminates: That should happen automatically. If it does not, some program launched by the batch file is still running.
start /b [bat file name]

Categories

Resources