Double Clicking JAR file does not open Command Prompt - java

I want to run a Jar file by double clicking it.
Following is the only Java class file present in it.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Sysout{
public static void main(String[] args) throws IOException{
System.out.println("Hello World!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg = br.readLine();
System.out.println(msg);
br.read();
}
}
And Manifest file has Main-Class defined.
Using this link, I successfully ran the Jar file by double-clicking the batch file.
This opens the command prompt and runs the main class defined.
However, if I double click the Jar file directly, nothing happens.
I also checked this link and associated my .jar to javaw.exe
This link also suggests the same.
Also tried by associating the .jar with java.exe
What happens is the command prompt opens for a fraction of second and vanishes off.
Even if I am expecting the user to enter some data, double-clicking operation does not wait for the user to enter anything.
Where is the problem?

When you use the javaw association, it does not create a command window, and swallows all the System.out and System.err invocations.
You should reassociate your .jar file with the java binary, which should display the requisite command window.
If you used the simple Open With... option, it will have omitted the -jar option from the command line.
Open up an administrator command window (this is needed if you're using Vista or Windows 7 with UAC enabled) and do:
assoc .jar=jarfileterm
ftype jarfileterm="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
In your case, you should replace the C:\Program Files\Java\jre7\bin\java.exe path with the one for your install of the jre.
When you double-click following this, then it should run correctly.
You can add another ftype:
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
again substituting the path to the javaw binary with the one that's for your system.
You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm and assoc .jar=jarfile
If you want to keep the command window around after running the .jar, then you surround the calling of the java command with a cmd /s /k viz:
ftype jarfileterm=cmd /s /k ""C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*"
assoc .jar=jarfileterm
If these commands worked, then double clicking on the jar file will cause a command window to pop-up and persist.
You cannot set a complex enough command line with either Open With... or using Default Programs that will allow the jar file to run. If you have successfully tried all these efforts ftype and assoc commands and it still doesn't work, then you will need to peel out the registry editor.
Launch regedit, and search for a key called .jar under HKEY_CLASSES_ROOT - this should result in a single value underneath it called (Default) with a value, if your ftype command invocations worked, then it should read jarfileterm. If it didn't work, then you're looking at an association that may have been created by another application (I don't know if the java updater replaces these entries, but if it does, then this could be the issue)
You need to next look for this key in the HKEY_CLASSES_ROOT. It will find this entry, which should contain the a key Shell (i.e. expand the folder jarfileterm and it should reveal another folder Shell), which contains a key Open which contains a key Command which contains a (Default) value that should contain the invocation command for launching .jar files. This command should match the last ftype jarfileterm=... entries that you typed in. If it doesn't then you should make it match one of the cmd /s /k or "c:\program files\java\jre7\bin\java.exe" options (depending on if you want to persist the command window in the event of an error in launching or not)

May be your .jar file has binded with any other default program, Right click and open with 'Java(TM) Platform SE binary'. This should work if have a executable jar file.

I am using the JDK to open the jar file in Windows 10.
Open regedit → HKEY_CLASSES_ROOT\jarfile\shell\open\command
Change the default from javaw to java
For example, mine is "C:\Program Files\Java\jdk-14.0.1\bin\java.exe" "-jar" "%1"

Related

JavaC not initializing when "Open Command Window Here"

When I try to shift+right-click a folder window, and click on: Open command window here, it brings up a command window, already cded to the path, like normal. I do have JavaC installed, and working, from the command line using javac, however opening the cmd window from Open command window here, and typing javac, it says:
'javac' is not recognized as an internal or external command,
operable program or batch file.
but it normally shows the help, without Open command window here.
.
My path variable is: C:\Windows\system32;C:\Windows;C:\Program Files\------;C:\Program Files\Java;C:\Program Files\Java\jdk-11.0.2;C:\Program Files\Java\jre1.8.0_201;C:\Windows\system32;C:\Windows;C:\Program Files\CCleaner;C:\Program Files\Java;C:\Program Files\Java\jdk-11.0.2;C:\Program Files\Java\jre1.8.0_201;C:\Program Files\Java\jdk-11.0.2\bin;C:\Program Files\Java\jre1.8.0_201\bin
The path to Java and JavaC are in the path variable.
Maybe this will help?:
You need to make sure it's in the Path environment variable. Edit it (e.g., in Windows 10, go to Advanced System Settings -> Environment Variables, choose Path and edit it) and make sure you have something like C:\Program Files\Java\jdk1.8.0_192\bin in it.
Yes! I finally got it to work! You see the registry screenshot of the 'Open command window here' option, the command key's (Default) string is cmd.exe /s /k pushd "%V", I removed the /s part, so it's cmd.exe /k pushd "%V", then I restarted the PC.

Windows 7 how to make explorer open a file with a vbs script

I am running openjdk on windows 7 without admin rights
I went through the explorer "open-with" dialogue to select java as the program to "open" the .jar file.
To run a .jar file by (double) click, windows executes something like
java (filename).jar
However, java requires the argument -jar, i.e.:
java -jar (filename).jar
To set this up the user needs admin rights to use assoc and ftype,
or implement the register edits as explained in the answer below.
Another workaround is to use a batch file, e.g. javastart.bat:
Listing javastart.jar
start java -jar %1
After going through the explorer "open with" dialogue, this works.
Clicking the jar file will open the command window and this will start java.
However, while java is running, the command-window is also open, which is ugly.
Edit
javaw.exe must be called, and the command-windows will close:
start javaw.exe -jar %1
the following script is not needed to close the command window
End of edit
To resolve this, I start a vbs script.
New listing javastart.jar
start startjar.vbs %1
And startjar.vbs:
Set args = Wscript.Arguments
cmd = "java -jar " & chr(34) & args(0) & chr(34)
Set WshShell = CreateObject("WScript.Shell")
CreateObject("Wscript.Shell").Run cmd, 0, True
This works: now I get a short flash of the command window starting the vbs, and then the windowless vbs-script starts java and the jar-file.
However, when I directly open the jar file with startjar.vbs, (right click the jar file, than go through the open with dialogue), the name of the jar file is not passed as an argument to the vbs, but windows tries to run the jar file directly (and gives an error: "the .jar file is not a valid win32-application").
Why is windows explorer not sending the filename as an argument to the vbs script?
Setting under HKCR come from both HKCU\Software\Classes and HKLM\Software\Classes. You can certainly edit HKCU settings as they are yours. If they exist they override the exact same HKLM settings. Under HKCR you will see a merged view of both with CU overriding any LM settings.
This started off as notepad's settings. Fix the path to your javaw.exe file.
jar files are now on the New menu, are searched by windows search, treated as text files so edit on right click menu but open as a program file.
If you are a non-admin use the reg command to merge (after fixing the path). If you are an admin double-click it.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile]
#="Java Program File"
[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell]
[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open]
[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open\command]
#="C:\\Folder\\javaw.exe -jar \"%1\""
[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar]
#="jarfile"
"Content Type"="text/plain"
"PerceivedType"="text"
[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\PersistentHandler]
#="{5e941d80-bf96-11cd-b579-08002b30bfeb}"
[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\ShellNew]
"ItemName"=hex(2):6a,00,61,00,72,00,66,00,69,00,6c,00,65,00,00,00
"NullFile"=""

Executable jar not running? Windows 10

I am having various .jar files in my system.
I have the JDK and JRE installed.
Most of jar files run on double clicking, but there are 2 - 3 jar files which do nothing on clicking. Help me.
By the way I am using windows 10 64 bit
Problem:
I could not start JAR files just by clicking on them in Windows 10.
I was messing around with the "control panel" ... forget it.
What I've found out:
Start a command line (cmd) as an Administrator
Check your file type association:
ftype jarfile
assoc .jar
I had to change my java path to a different one
ftype jarfile=C:\myjavapath\javaw.exe -jar "%1" %*
Which basically means, that if someone starts a jar file, the command would be:
C:\myjavapath\javaw.exe -jar "example.jar" parameter1 parameter2
For me, I also had to change my .lnk files to only contain the name of the jar file, not the whole command. Type in the whole path of the jar file in the "target" field in the properties of the link file (.lnk).
You can debug it in the Command Prompt.
Open start, type in CMD.exe, hit enter
Then, type in
java -jar "path\to\file.jar" without the quotes
or
java "path\to\file.jar"
You should be able to see an output log of what is happening that is making the jar file not execute properly
Your Manifest file should contain a line:
Main-Class: classname
See "Setting an Application's Entry Point".

Associate File Extension with Java Application

Here is a picture of the 2 files in question, one .atb and one .jar
If i just click the jar file, it opens my program smoothly, no questions asked.
If i click the New Text Document and choose "y" as my default program, it says this:
If i do all this with .txt as file extension it says the same, still doesn't work.
If i do all this in Windows 7 with the same setup, it all works fine.
Also i checked my event logs when this occurs and this pops up as a keyword "Audit Success" with the text: "An attempt was made to query the existence of a blank password for an account."
Do you have any idea what might cause this?
You can't associate file extensions to trigger a .jar file on Windows. The only file types you can trigger on Windows are .exe, .pif, .com, .bat, .cmd, so instead of triggering the .jar file, you can trigger a .bat file, which then launches the .jar file.
Create a y.bat file and place it next to your y.jar file and write the following code inside it:
#echo off
title y
start javaw -jar "C:\Users\SomeUsername\Desktop\y.jar" %1
You can change the title and y.jar path as you please, just remember that the path need to be the absolute path. Though the real keyword here is %1, that is the actual path, of the file you clicked.
You can get the value of any parameter using a % followed by it's numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on
%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)
Now you can simply right-click on any .abt file and press "Open With...", remember to check "Use this app for all .abt files" and then just browse for the y.bat and click "Open". Now each time you double-click a .abt file it will launch your .jar program.
Additionally I've written this post (Associate File Extension with Java Application) after writing this answer.
You can get by without a bat file by running from the command line (with admin privileges)
assoc .xox=Xoxfile
ftype Xoxfile=C:\Program Files\Java\jre1.5.0\bin\javaw -jar c:\dev\work\y.jar %1
(correcting for the path to your current jre and path to your jar).
Since .txt is already a defined filetype in windows, I think you could skip the assoc command and replace Xoxfile above with
ftype "Text Document"=C:\Program Files\Java\jre1.5.0\bin\java -jar c:\dev\work\y.jar
but I've only done this with a new custom file type.
https://www.rgagnon.com/javadetails/java-0592.html
I had a silly issue with a .JAR opening. After running "java -jar MyApp.jar", I came to the realization that it was not launching the gui as my preferences file was not in the same path as the .jar file.
Adding a check for if(!file)then create new file... the app worked as expected and became easier to port/share.

Running JAR file on Windows

I have a JAR file named helloworld.jar.
In order to run it, I'm executing the following command in a command-line window:
java -jar helloworld.jar
This works fine, but how do I execute it with double-click instead?
Do I need to install any software?
Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).
Or this:
Open the Windows Explorer, from the Tools select 'Folder Options...'
Click the File Types tab, scroll down and select JAR File type.
Press the Advanced button.
In the Edit File Type dialog box, select open in Actions box and click Edit...
Press the Browse button and navigate to the location the Java interpreter javaw.exe.
In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" % (Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.
Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
In Windows Vista or Windows 7, the manual file association editor has been removed.
The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back... double-clickable again.
If you need to distribute your .jar file and make it runnable at other people's Windows computers,
you can make a simple .bat file like this in the command prompt:
java -jar MyJavaTool.jar
and place the .bat file in the same directory as your .jar file.
If you have a jar file called Example.jar, follow these rules:
Open a notepad.exe
Write : java -jar Example.jar
Save it with the extension .bat
Copy it to the directory which has the .jar file
Double click it to run your .jar file
An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.
If you try (in a command prompt):
jarfile.jar parameter
No joy, because this is being translated to the following (which doesn't work):
javaw.exe -jar jarfile.jar parameter
However, the following command does work:
java.exe -jar jarfile.jar parameter
If you change the association in file manager as described above to:
"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*
Then you can type:
jarfile.jar parameter
in the command prompt and it will now work!
EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)
If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a "press a key" function at the end or the window will just disappear.
In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.
There isn't a single, consistent solution here, but you would have the same problem with any other console application.
There is a windows freeware application called "bat to exe" which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.
First set path on cmd(command prompt):
set path="C:\Program Files\Java\jre6\bin"
then type
java -jar yourProgramname.jar
In Windows XP * you need just 2 shell commands:
C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %*
C:\>assoc .jar=myjarfile
obviously using the correct path for the JRE and any name you want instead of myjarfile.
To just check the current settings:
C:\>assoc .jar
C:\>ftype jarfile
this time using the value returned by the first command, if any, instead of jarfile.
* not tested with Windows 7
In regedit, open HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\command
Double click on default on the left and add -jar between the java.exe path and the "%1" argument.
There is way without requiring user to do changes on his PC. Runtime.getRuntime.exec() allows us to start cmd.exe and execute commands inside of it. So, it's possible for java program to run itself in command prompt when user clicks .jar file.
public static void main(String[] args) throws IOException {
if(args.length == 0) {
Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
} else {
//code to be executed
}
}
Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.
I´m running Windows 7 x64 and was unable to use any of these fixes.
This one worked for me afterall:
http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi
There is an archive which you can download containing a .bat file to run, but check the path of the actual javaw.exe!!!!
You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.
The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.
Unfortunatelly, it is not so easy as Microsoft has removed advanced file association dialog in recent Windows editions. - With newer Windows versions you may only specify the application that is going to be used to open .jar file.
Fixing .jar file opening on Windows requires two steps.
Open the Control Panel, and chose "Default Programs -> Set Associations". Find .jar extension (Executable JAR file) there, and pick Java as default program to open this extension. It will probably be listed as "Java Platform(SE)". A faster alternative perhaps is straightforward right-click on a .jar file, and then change associated program by clicking on the "Change..." button.
Now open the regedit, and open the HKEY_CLASSES_ROOT\jarfile\shell\open\command key. Luckilly for us, we may specify parameters there for the (Default) value. On my Windows system it looks like: C:\app\32\jre7\bin\javaw.exe" -jar "%1" %* but in most cases it is the following string: C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
NOTES:
Do not use java.exe there as it will open the shell window.
The jarfix tool mentioned in this thread most likely does nothing more than the registry modification for you. I prefer manual registry change method, as that implies that system administrator can "push" the registry change to all workstations in the network.
Create .bat file:
start javaw -jar %*
And choose app default to open .jar with this .bat file.
It will close cmd when start your .jar file.
Got the same problem, on Windows 10
The solution:
Check your JAVA_HOME and JAVA_PATH.
https://javatutorial.net/set-java-home-windows-10
Use Jarfix to restore the assiciation between .jar and javaw.exe
https://johann.loefflmann.net/en/software/jarfix/index.html
I had the same problem in Windows 10. I fixed it using righ-click on the "helloworld.jar" and go to properties and click on change button under "Opens with:" and select "Look for another app on this PC". In the "Open with..." dialog box, go to your Java folder location on your PC and open corresponding jdk folder and then open the bin folder and select "javaw.exe" from there. Then next time your "helloworld.jar" will open the normal way.
Usual java location example : "C:\Program Files (x86)\Java\jdk1.8.0_111\bin".
Another way to run jar files with a click/double-click, is to prepend "-jar " to the
file's name. For example, you would rename the file MyJar.jar to -jar MyJar.jar.
You must have the .class files associated with java.exe, of course. This might not work in all cases, but it has worked most times for me.
PreScript: If your prompt appears and disappears immediately, the reason it does so is that your program gets executed and auto shut. Try putting a scanner in the end to terminate and it'll keep your prompt waiting for input before terminating. (Or use delay maybe)
Was in the very same situation, where running .jar from cmd was working fine, but double clicking did nothing.
Solution:
Open any text editor and write the command line:
java -jar Example.jar
Save the file as a .bat file.
Run this bat file to get the needed output.
Taking it one step forward, you can convert this bat file to exe file using a simple GUI tool like Bat To Exe Converter.
Now you can share your .jar as a distribution in .exe file which anyone can use just make sure you keep all the files together. (Especially the .jar and .bat file cause .bat is only a cmd prompt)(How it feels logical)
I am fairly new to development and learning a lot. Please excuse for any mistakes if committed. Suggestions are welcome.
If you use eclipse for making your java files, you can choose to export it as a runnable jar file. I did this with my programs and I can just click on the jar and it will run just like that. This will work on both windows, as well as os x.
Making a start.bat was the only thing that worked for me.
open a text document and enter. java -jar whatever yours is called .jar
save as start.bat in the same folder as the .jar file you want to execute. and then run the. bat
If you need to run the jar file by double clicking on it, you have to create it as a "Runnable JAR". you can do it simply with your IDE.
If you're using eclipse, follow these steps :
To create a new runnable JAR file in the workbench:
1.From the menu bar's File menu, select Export.
2.Expand the Java node and select Runnable JAR file. Click Next.
3.In the Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
4.In the Export destination field, either type or click Browse to select a location for the JAR file.
5.Select an appropriate library handling strategy.
Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.
more information can be found on Eclipse help Page: LINK
There are many methods for running .jar file on windows. One of them is using the command prompt.
Steps :
Open command prompt(Run as administrator)
Now write "cd\" command for root directory
Type "java jar filename.jar"
Note: you can also use any third party apps like WinRAR, jarfix, etc.
Steps:
1.) search for Java SE Runtime Environment on Google: https://www.google.com/search?q=Java+SE+Runtime+Environment
2.) install the appropriate version onto your computer
For compiling:
javac -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java
For running:
java -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart
use .bat file:
Put your command in a .bat file. here, your command will be java -jar path\yourJarName.jar.
Something like: java -jar C:\workspace\myApplication.jar
Save it and double click on bat file to run your jar.
Actually, I faced this problem too. I got around it by making a .bat runner for my jar file.
Here is the code:
class FileHandler{
public static File create_CMD_Rnner(){
int exitCode = -1625348952;
try{
File runner = new File(Main.batName);
PrintWriter printer = new PrintWriter(runner);
printer.println("#echo off");
printer.println("title " + Main.applicationTitle);
printer.println("java -jar " + Main.jarName + " " + Main.startCode );
printer.println("PAUSE");
printer.flush();
printer.close();
return runner;
}catch(Exception e){
System.err.println("Coudln't create a runner bat \n exit code: " + exitCode);
System.exit(exitCode);
return null;
}
}
}
Then in Your Main application class do this:
public class Main{
static String jarName = "application.jar";
static String applicationTitle = "java Application";
static String startCode = "javaIsTheBest";
static String batName = "_.bat";
public static void main(String args[]) throws Exception{
if(args.length == 0 || !args[0].equals(startCode)) {
Desktop.getDesktop().open(FilesHandler.create_CMD_Rnner());
System.exit(0);
}else{
//just in case you wanted to hide the bat
deleteRunner();
// Congratulations now you are running in a cmd window ... do whatever you want
//......
System.out.println("i Am Running in CMD");
//......
Thread.sleep(84600);
}
}
public static void deleteRunner(){
File batRunner = new File(batName);
if(batRunner.exists()) batRunner.delete();
}
}
Please Note that
this code (my code) works only with a jar file, not a class file.
the jar file must have the same name as the String "jarName" is the Main class

Categories

Resources