Related
I wrote a java app called "MyApplication" in IntelliJ with OpenJDK. It displays a JFrame on the desktop until the user closes it. I've exported it as a .jar file with default artifact options. But if I try to start it with a double-click unfortunately nothing happend.
I tried to change the application to start with to java.exe and javaw.exe from the OpenJDK installation folder but that doesn't help. I also checked my environement variables from my windows system. I set them all like every webside from google says, they have to be correct. If I run javaw MyApplication.jar from command prompt nothing happens too. But if I run java MyApplication.jar from command prompt following error message occurs:
Error: Could not find or load main class MyApplication.jar
Caused by: java.lang.ClassNotFoundException: MyApplication.jar
After a research I found out that a classpath could be missing. But I do not use any external libraries or other stuff. Just the OpenJDK and a JFrame object from the java.swing library. A second reason could be that there is a mistake in the 'Manifest.mf' file but that was created by IntelliJ and I checked it. This file should be ok. Here is a preview:
Manifest-Version: 1.0
Main-Class: de.java.configuration.Main
deis the first package in src folder. java the second and so on. Main is the java class where the main method is included. If I set a -jar between java / javaw and MyApplication.jar in command prompt it runs perfect.
At this moment I want to ask an other question. What exactly -jar means as java parameter in command prompt?
I can't open other .jars too. The same behaviors occurs.
So my target is that I can double-click the .jar on my desktop for example and the app opens. Like javaw -jar MyApplication.jarbut with a double-click on the software icon. How is it possible?
I want to execute my program without using an IDE.
I've created a jar file and an exectuable jar file. When
I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:
Error: Unable to access jarfile <path>
I use the command: java -jar Calculator.jar
How I created the jar:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
I had encountered this issue when I had run my Jar file as
java -jar TestJar
instead of
java -jar TestJar.jar
Missing the extension .jar also causes this issue.
Fixed
I just placed it in a different folder and it worked.
[Possibly Windows only]
Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:
java -jar myjar.jar
I was able to fix this by givng the full, quoted path to the jar:
java -jar "%~dp0\myjar.jar"
Credit goes to this answer for setting me on the right path....
I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.
I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:
java -jar C:\Path to File\myJar.jar
i had to write
java -jar "C:\Path to File\myJar.jar"
Just came across the same problem trying to make a bad USB...
I tried to run this command in admin cmd
java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
But got this error:
Error: unable to access jarfile c:\fw\ducky\duckencode.jar
Solution
1st step
Right click the jarfile in question. Click properties.
Click the unblock tab in bottom right corner.
The file was blocked, because it was downloaded and not created on my PC.
2nd step
In the cmd I changed the directory to where the jar file is located.
cd C:\fw\ducky\
Then I typed dir and saw the file was named duckencode.jar.jar
So in cmd I changed the original command to reference the file with .jar.jar
java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
That command executed without error messages and the inject.bin I was trying to create was now located in the directory.
Hope this helps.
None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.
Fixes:
Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.
The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.
Do the following steps:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
Then type java -jar filename.jar
This should work.
Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
java filename.java
If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.
to check this you can view extended attributes on the file
$ ls -l#
-rw-r--r--# 1 dave staff 17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms 619
com.apple.quarantine 68
You can then clear the attributes with
xattr -c file.jar
It can also happen if you don't properly supply your list of parameters. Here's what I was doing:
java -jar test#gmail.com testing_subject file.txt test_send_emails.jar
Instead of the correct version:
java -jar test_send_emails.jar test#gmail.com testing_subject file.txt
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
For me it happens if you use native Polish chars in foldername that is in the PATH.
So maybe using untypical chars was the reason of the problem.
sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work
My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.
Disregarding relative file paths fixed it right up.
In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved
It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ".
For example:
java -jar c:\my folder\x.jar -->
java -jar "c:\my folder\x.jar"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.
Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.
"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar
If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).
A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?
For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.
So i used:
%Java8_64%\bin\java -jar myjar.jar
Instead of java 7 version:
java -jar myjar.jar
I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.
Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.
this is because you are looking for the file in the wrong path
1. look for the path of the folder where you placed the file
2. change the directory cd in cmd use the right path
I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!
Maybe you have specified the wrong version of your jar.
I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
Rename the jar file and try
Explanation :
yes, I know there are many answers still I want to add one point here which I faced.
I built the jar and I moved it into the server where I deploy (This is the normal process)
here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue.
maybe at the time of overriding there can be a permission copy issue.
Hope this will help someone.
Have you tried to run it under administrator privoleges?
meaning, running the command in "Run As" and then select administrator with proper admin credentials
worked for me
I was trying this:
After giving the file read, write, execute priviledges:
chmod 777 java-repl.jar
alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"
Unable to access bla bla..., this was on Mac OS though
So I tried this:
alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"
This did not work "Unable to access jarfile"
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"
This does work
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"
The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.
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".
I have made a simple game that I want to convert into a runnable jar so I can show others and launch it without Eclipse.
In Eclipse I:
Right clicked on Project
Export
Java > Exectuable Jar File
Launch Configuration: CTB (1) - Click The Block
It made a jar with a MANIFEST.MF containing:
Manifest-Version: 1.0
Class-Path: .
Main-Class: uk.co.robertmerriman.ctb.main.CTB
This was all extracted to my desktop in Click-The-Block.jar
When I double click, nothing happens.
When I type "java -jar Click-The-Block.jar" into CMD, I get the following error:
Error: Unable to access jarfile Click-The-Block.jar.
Make your jar file name without spaces locally, then write in cmd like the following:
java -jar YourjarFileNameWithoutSpaces.jar
Looks like an OS issue. If you are running windows 07 or vista try running the same in administrator mode.
Also make sure that when you are running the command
java -jar Click-The-Block.jar
You are running it from the directory where the jar is located.
if your java.exe is in c:\java\bin\java.exe and your Click-The-Block.jar is in c:\dev\
Then try out the following command
>
c:\java\bin\java -jar c:\dev\Click-The-Block.jar
Also try renaming your jar file with no cps letter, i know this will not have any effect but there's no harm in trying.
If you are still not able to execute the jar then try downloading an executable jar file from the net and see if you are able to run it using the standard java command. If you still get the same error which means the problem is not with your jar but somthing to do with how java is configured in the system. In that case you can go ahead and reinstall a fresh jdk and try al over again.
Hope it helps ...
I had the same issue with my executable jar generated by maven-shade-plugin "DCD-Desktop-1.0.jar" , I removed the dashes and renamed it to "dcd.jar" and it worked fine .
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