So I've been writing this little code, and it works -- but I have to type in the whole Computer;~ User$ java MyProject and change the path every time, so I decided to stick it in a .jar file (so I could just run it on a double click). I am an Intellij user on running macos. I created the file, File/Project Structure/artifacts/+/myMain etc and now have a .jar file sitting on my desktop. When I run the jar inside of Intellij, or run the jar using
java -jar /Users/Me/Desktop/MyProject/out/artifacts/MyProject_jar/MyProject.jar, it works fine.
However, when I:
Run it from the terminal with java -jar MyProject.jar I get Error: Unable to access jarfile MyProject.jar
^^Resolved - using the chmod 755 MyProject.jar command, see comment for further details
Or run it on right click with Jar launcher I get The Java JAR file "MyProject.jar" could not be launched. Check the Console for possible error messages. There are no error messages in the console.
And before anyone says it, I have Java 13 and the project is running on Java 13 -- it's the default that my computer has, and has been updated automatically ever since installation.
How would I be able to launch this file, and what could be stopping it from doing so?
in terminal, do an
ls -l MyProject.jar
You'll see something like:
-rw-r--r-- 1 youruser yourgroup 1024 Apr 24 15:41 MyProject.jar
The -rw-r--r-- part is the file permissions where an "r" means readable, "w" means writable, and an "x" would mean executable, but as you can see there isn't one there. Without going into the long explanation, use the chmod command to make the file executable by doing
chmod 755 MyProject.jar
Afterwards, use the same ls command and you should see -rwxr-xr-x in the permissions field. The file is now executable (by anyone) and should launch if you click in in Finder.
If your program has no GUI but requires console input, you should run it from the terminal instead of double clicking it. When you double click the MyProject.jar file, you actually launch it through jar launcher. The jar launcher will pass the files to the JVM. However, since you are not launching it from the terminal, the JVM does not know which terminal to use, so the program might not be properly executed.
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 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'm having a strange issue trying to run classes from an executable .jar file on Linux that none of the existing question threads I've sorted through seem to be able to resolve. I'll preface this in that I've never had to use Linux before and am only using it in this situation out of necessity, so it's possible I have overlooked something simple that I just didn't know could be causing the problem.
I can launch the classes from my .jar file without any issues on Windows via a .bat file with the following settings:
start "MyServer1" java -classpath ./*;Server.jar infoServer/StartInfoServer
start "MyServer2" java -classpath ./*;Server.jar loginServer/StartLoginServer
start "MyServer3" java -classpath ./*;Server.jar chatServer/start
start "MyServer4" java -classpath ./*;Server.jar gameServer/start
However, when I move to trying to launch these classes from the .jar on Linux, I get a "could not find or load main class" error. My .sh file is set up like this, and is placed in the same directory as my .jar file:
echo Starting Servers
java -cp Server.jar infoServer.StartInfoServer
java -cp Server.jar loginServer.StartLoginServer
java -cp Server.jar chatServer.start
java -cp Server.jar gameServer.start
echo All Done Starting Server
I've used ls from the Terminal to verify the .jar and .sh were being recognized as existing where they should be. (For future note, I'm using the Terminal from inside the directory containing my files.) I've made sure to make use of chmod to be sure both the .jar and the .sh have read/write/execute permissions and used ls -l to verify those permissions were indeed present. I've tried various forms of explicitly defining the classpath, such as using "/home/machine/Desktop/Folder/MyJar.jar", using pwd from the Terminal to ensure I'm getting the filepath correct. I've checked over my Java compatibility. (1.7.0_65 on Linux, 1.8.0_45 on Windows, with the .jar being created in Eclipse using 1.7 Compliance settings.) I can use unzip MyJar.jar from the Terminal and it will properly extract all my class files, allowing me to verify that my .jar isn't corrupted on my Linux machine and that the paths to the classes I'm trying to run are still the same on Linux as they are on Windows.
I do apologize if this is just a problem of inexperience overlooking something, but I can't think of or find any indication of what the problem could possibly be.
EDIT:
I've updated the question with some screenshots related to the problem:
https://gyazo.com/0ae2a2701aae734db21ef7c29200283b - General File Setup.
https://gyazo.com/d735d9cee57b4a92078c4b624d012b8c - Running the Shell via Terminal.
Other notes: jar -tf Server.jar works from the Terminal but not from inside the Shell script, which leads me to believe this may be some kind of visibility or pathing error, but I can't find any reason why that would be the case.
I'm using Windows, and my _vimrc file has the following lines:
autocmd Filetype java set makeprg=javac\ -d\ %:~:h:s?src?bin?\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>
map <F12> :!start cmd /k "java %:~:s?src?bin?:r"
I just want to use this for quick editing, and I will be using an IDE most of the time, thus the need for the classic /src/ and /bin/ folders (they are at the same folder level).
So it happens like this:
- I open the .java file that I want to edit in vim (it is in the /src/ folder)
- I hit F9 and javac runs on my current file (%) and puts the class files in the /bin/ folder (this seems to work as expected), and a new error window is opened in vim to display compile errors
- I hit F10/F11 to cycle between errors
- I hit F12 to open a command prompt and execute the java program (I think I have to open the command prompt because my program asks for user input in the console), except it gives an error in the command prompt that it could not find or load the main class
I don't know why it won't work, the class paths seem correct. Can you show me where I have went wrong?
Edit: #merlin2011 led me to the following answer:
map <F12> :!start cmd /k "cd %:~:h:s?src?bin? & java %:r"
I had to cd to /bin/ and then run java.
Or: without changing directory, and just changing the classpath:
map <F12> :!start cmd /k "java -classpath %:~:h:s?src?bin? %:r"
For permanency, I am converting my comment into an answer.
In order run the Java command from vim, one must first cd to the directory bin and run the Java command from there.
map <F12> :!start cmd /k "cd %:~:h:s?src?bin? & java %:r"
I wrote a little command-line game that has 5 classes: the main class "DiaDia.class", and the others "Partita.class", "Comando.class", "Stanza.class", "Attrezzo.class".
Then i created a diadiamanifest.txt file so:
Main-Class: DiaDia
ending file with a new line.
I tried to make a DiaDia.jar file with 2 different commands:
jar cvfm DiaDia.jar diadiamanifest.txt DiaDia.class Partita.class Comando.class Stanza.class Attrezzo.class
jar cvfe DiaDia.jar DiaDia DiaDia.class Partita.class Comando.class Stanza.class Attrezzo.class
In both cases the generated DiaDia.jar file runs well with the command:
java -jar DiaDia.jar
but does nothing with a double click on it.
I set properly the file association of .jar extension to
C:\Program Files\Java\jre7\bin\javaw.exe
I run Windows 7 Home Premium x64, with jre7 update 76 and jdk1.7.0_76.
I wrote a little command-line game
That's the problem then.
javaw.exe is designed to run GUI applications - it doesn't allocate a console. If you change the file association for .jar to run java.exe instead, it will launch a console and your app will run. On the other hand, anything else that's designed to run as a launchable jar file - most of which will have GUIs - will end up launching a console even if you don't want one.
Alternatively, change your game to not need a console, or just run it from a command line using java -jar ... which, as you've already said, works fine.