How to open/run java / .jar minimized by using cmd console? - java

I found different possibilities to open Windows applications minimized from cmd.exe. Best related topics I found were:
open program minimized via command prompt and
Run Java console app as a daemon (background)
What I couln'd get working is running a .jar from cmd in a minimized or background state. The window still pops up in front with, e.g. :
START /min javaw -jar App.jar &
or
START javaw -jar App.jar --new-window/min
or other variations of these cmd options.
I made a workaround by setting it up in the application itself with
frame.setState(Frame.ICONIFIED)
But does anyone know how to do this by cmd, or what could be the problem (Win7 x64)?

Related

Gnome desktop file does not run "java -jar" command

I wrote an application with JavaFX and I would like to run it like a "native" app in Ubuntu.
So I created the following desktop file and put it in ~/.local/share/applications:
[Desktop Entry]
Name=Notes
Exec=java -jar /home/namhto/dev/Notes/build/libs/notes-1.0.0.jar
Icon=/home/namhto/dev/Notes/build/libs/icon.png
Type=Application
Terminal=false
The desktop icon is created but when launching it from the gnome desktop, nothing happens. Running the command java -jar /home/namhto/dev/Notes/build/libs/notes-1.0.0.jar from a terminal effectively starts my app.
I tried to mark the jar file as executable in Ubuntu but it does not change anything.
I would start with using /the/absolute/path/to/java for your exec command (which you can find out via which java for example), instead of a relative one. Maybe your ENV setup doesn't have java in $PATH somehow.
Anything else looks good to me.

Does command-line stop programs launched in command window when command window is closed?

When launching and executable jar from the command window, is the jar attached to that window? So when I close the command line the program stops running, or will it continue in the background?
If so, System.out.println() should that print to the command window open if I'd closed the first command window?
This is in Windows (server 2012)
java.exe is attached to the current running command shell or the batch script that launched it. javaw.exe will start it as a separate windows program without that attachment. Using javaw is approximately the same as running java -jar xyz.jar & on Linux.

Execute command in a separate cmd or terminal

It is possible to execute external commands in java at run time.
However, what I am looking for is to actually open cmd or terminal and show that executing external command on screen.
I tried to execute a jar console application as java -jar jartool arguments, it works both on Windows and Ubuntu. The only problem is that, this does not open cmd or terminal separately. it runs the command in background.
We can use either ProcessBuilder and the start process or simply Runtime.getRuntime().exec.
I need to forcefully open the cmd or terminal. Java is configured to show console on windows but still no luck.
Any idea why this is happening or what should I do to force it to open cmd or terminal independent of current cmd or terminal.

Difference between java and javaw

I searched to know the difference between java.exe and javaw.exe. I read through
Difference between Java.exe and Javaw.exe.
There it is stated that java.exe is for console and javaw.exe is for window application.
In some other post it is mentioned that console is not available in javaw.
but I wonder when I run Tomcat server and see its process in process explorer I see javaw.exe even though tomcat is a console application.
The java and javaw commands states
The java and javaw tools start a Java application by starting a JRE and loading a specified class.
The javaw command is identical to java, except that javaw has no
associated console window. Use javaw when you do not want a command
prompt window to be displayed.
The javaw launcher displays a window with error information if it fails.
In case of Tomcat you won't see Win32 console application running, similarly to launch Eclipse, javaw.exe is used.
Example :
Write the following code :
import javax.swing.*;
public class JavavsJavaw {
private static void renderGUI() {
JFrame jFrame = new JFrame("HelloWorld Swing");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel helloLabel = new JLabel("Hello World!");
jFrame.getContentPane().add(helloLabel);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
renderGUI();
}
});
}
}
Step 1 : C:\>java JavavsJavaw
(the command-line waits for the application response till it closes)
Step 2 : C:\>javaw JavavsJavaw
(the application launches and the command line exits immediately and ready for
next command)
1. java.exe is the command where it waits for application to complete untill it takes the next command, javaw.exe is the command which will not wait for the application to complete. you can go ahead with another commands.
2. java.exe is the console app while javaw.exe is windows app (console-less), The javaw.exe command is identical to java.exe, except that with javaw.exe there is no associated console window
Difference between java.exe and javaw.exe commands is while running java program on windows you might have noticed that they will appear in task manager as either java.exe or javaw.exe, also in JAVA_HOME/bin we see these two java commands java.exe and javaw.exe, do you know difference between java.exe and javaw.exe
Both java.exe and javaw.exe can execute java programs including jar file, only difference is that with java.exe you have a console executed and you need to wait until your java program finishes to run any other command on the other hand in case of javaw.exe no console or window is associated with execution.
you can run your java program either by using java.exe or javaw.exe. Its suggested to use javaw.exe when you don't need command line window to appear. javaw launcher will display in error in a dialog box if execution fails.
In summary main difference between java and javaw command is that with java.exe you will get a command prompt window but with javaw.exe there is no command prompt windows associated.
java.exe pops up a console window. javaw.exe does not.
Both java.exe and javaw.exe can execute java programs including jar file, only difference is that with java.exe you have a console executed and you need to wait until your java program finishes to run any other command on the other hand in case of javaw.exe no console or window is associated with execution.
Main difference between java and javaw command is that with java.exe you will get a command prompt window but with javaw.exe there is no command prompt windows associated.
It depends on how the Tomcat server is run. However, the way of configuring and starting Tomcat is out of the question. I could say that there's different options of doing that. If you starting Tomcat with java it runs like command line application which directs IO to the console. If you run it with javaw then it doesn't directs IO to the console. Many applications and servers has logging capabilities that could direct the output to the console. For this purposed better to use a command line instance of java instead of windows app. There's a reason to run javaw vs java is no logging, debugging messages, no error, stacktraces, exceptions, everything that System.out and System.err produce is visible while you running the application.
In addition to the points stated, it is also can be noted that multi threaded programs can be run with java but javaw can run only single threaded programs!

Start a java program without the console

I am trying to use this GUI mod for a Minecraft Server. I wrote a batch file so the server can start with more RAM. When I run just the .jar file, no command window opens and it runs just fine (of course with about 256mb ram) I was reading online that javaw starts a jar file without a command-line-console. But when I use javaw, the command console opens, but when I close it the program remains open. this is my batch file:
#echo off
"%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe" -jar -Xms1024m -Xmx1024m crafty.jar
#echo on
I don't understand java as well as most, so please try to be as clear as possible. Thanks
If you want to start a java program without console popup under windows, this should be helpful:
In command prompt type the following:
start javaw -jar -Xms1024m -Xmx1024m crafty.jar
If you want you can also write this as a batch file.
You should Create Shortcut of "%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe", let's name it as Minecraft, then
edit the Properties of Minecraft shortcut. In the Target textbox, append -jar -Xms1024m -Xmx1024m crafty.jar in the end of javaw.exe
change the Start in as the folder which contains the crafty.jar
Double-click the Minecraft icon to star the server.
That's all.
Create a .bat file with
start javaw -jar yourjar.jar arg0 arg1
start javaw -jar yourjar.jar arg0 arg1
will open the console, but close immediately. it is different from running window .exe.
You will always get the command window opening and closing because you are starting it inside a command window or batch script (which launches an implicit command window to run itself).
In order not to get a command window you must open the file from "not a command window" i.e. an executable launcher.
Take a look at Launch4j which can run a java program from an exe. It can also hide-away the jar file inside the exe if you like.
http://launch4j.sourceforge.net/
There's a little YouTube clip showing them creating an exe from a jar.
A batch file is a way of starting the command prompt with the code pre-written, using javaw is a way of open then closing the prompt. Like I said a batch is a commands prompt you can't stop it from opening.
It's few years, but for windows today as for linux you have the supervisor (pytho based)
supervisor windows based on python

Categories