Run JAVA 8 jar on windows without command prompt - java

This is a simple Hello World test application.
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
try {
System.in.read();
} catch (IOException e) {
System.out.println("IOException : "+e.getMessage());
}
}
}
It was exported as "Runnable JAR File" in Eclipse.
Opening it using java -jar hello.jar on Command Prompt works.
However when trying to open it using double click, nothing happens.
Note 1 : Java Platform is the default program associated with this type.
Note 2 : java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
The problem Double click does not execute a command line JAVA 8 jar file.
How can i solve it?

If you doubleclick a *.jar it is actually started with javaw -jar Hello.jar. The difference between java.exe and javaw.exe is that with javaw there is no associated console window. As your test application do not show any GUI element, it looks like as it would not be executed.
Depending what you want to achieve you either create a script to run your application or you make some graphical interface.

When you execute the JAR from File Explorer, there's no shell for it to write the text output to. When you run it from the command line, it's already wrapped in a shell so the System.out stream is already set to the shell's output stream and so print statements are visible.
The simplest practical solution would be to create a batch file that performs the java -jar hello.jar command which would launch a command window when you open it in File Explorer.
Another solution would be to launch a cmd.exe context from within your Java application using Runtime.exec() and set your output stream to the same as the process object it returns.
The most elegant solution, but one which requires a bit more background work, would be to instantiate a simple GUI consisting of a JFrame and a JTextArea within a JScrollPane, then you would append your output to the text area.

Your jar may be opening with javaw.exe. You can verify that by executing command
ftype | find "jarfile"
If its showing as javaw.exe
ftype jarfile="C:\Program Files\Java\jdk1.8.0_45\jre\bin\javaw.exe" -jar "%1" %*
change it to java.exe
ftype jarfile="C:\Program Files\Java\jdk1.8.0_45\jre\bin\java.exe" -jar "%1" %*
I tried and it worked for me.

Related

java launch no console window in Windows 7

I've already seen this questions, but have no positive result:
1) Is there a way to the hide win32 launch console from a Java program (if possible without JNI)
2) Start a java program without the console
I've something like that:
"C:\Program Files (x86)\Java\jre1.8.0_71\bin\javaw" -Dfile.encoding=UTF-8 -cp ".;manyjarshere.jar" main.Main
And console appears, but I can close it manually, but I want it hidden by start.
If I place "start" before "C:..." then it shows "not found "-Dfile..."
start javaw [args] should do the trick IF java /bin folder is added to the system's PATH environment variable

System.out.println() not working in windows

Thank you for your replays.
I'm writing this code using eclipse on ububntu OS.
this is my code:
public class test{
public static void main (String[] args){
System.out.print("Hello world");
}
}
Then make it by using eclipse export JAR Executable file.
I run the code under ubuntu terminal like this:
java -jar test.jar
so I got the Hello world string, and this is the result:
Screen shot on ubuntu
And running the same command on the command prompt of windwos 8.1 x86.
java -jar test.jar
And the result is this: Result on windows
Thank you again for helping.
This might be the classpath issue , use below code to run. -verbose will print you the execution details.
set classpath=.;
java -verbose -jar helloworld.jar
Please show us your code and the way you run it.
First idea: make sure you run your code on the command line:
open DOS box
cd to the directory where your .jar file is
java -jar helloworld.jar
You might not see any console output if you double-click the .jar file

Unable to run HelloWorld.java from command prompt

I got a strange problem in my windows 7 system running jdk1.6.0_33
When I try to run a simple java program from command prompt, it opens a new window (something like java frame) and suddenly disappears. There is no result shown on command prompt also I am unable to terminate the process (using Ctrl+C) or close command prompt after this. A java process is created each time I do this. I tried to kill process using Task Manager, but that too didn't work.
I am able to run the same program using eclipse.
Here is my program
class HelloWorld{
public static void main(String [] args)
{
System.out.println("Hello World");
}
}
Environment variables are set as follows:
Path=C:\Program Files\Java\jdk1.6.0_33\bin
classpath=.
Commands I used are,
javac HelloWorld.java
java HelloWorld
Why is this happening? Thanks in Advance.
I'm not sure, but I think this is what happens if you use java versus javaw from the command prompt on Windows:
References:
The java command manual page for Windows.
Difference between java/javaw/javaws
If your java class path is configured correctly.
type javac command in command prompt
You will get java compiler information.
After compile the class find your folder location if any .class file is created.

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