trying to make executable .jar file

I coded a program in intellij with using maven, made an artifact and build it, and if I run the .jar file which gets created using cmd it works. Now I want to make it executable with a double-click without the need of an additional file, but when I try to run it that way an error message saying "A java Exception has occured." pops up, even tho it works when starting the file using java -jar file.jar in cmd. I also installed jarfix prior to this and run it as admin, it works for other .jar files which I needed it for, but doesn't help with my own program. What am I doing wrong?
No description
88 Replies
JavaBot
JavaBotβ€’2mo ago
βŒ› This post has been reserved for your question.
Hey @Lloyd_159! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. πŸ’€ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Madjosz
Madjoszβ€’2mo ago
Are you sure it packaged the correct JRE into your jar? This error often occurrs when you want to start some newer JARs with Java 1.8.0.
Lloyd_159
Lloyd_159OPβ€’2mo ago
how can I check which JRE I am using in my build? okay my I think I found it, In my project structure it says I'm using SDK 19. Tho when I run java -version in cmd, it says my current installed java version is 20.0.2, so I should be fine?
Madjosz
Madjoszβ€’2mo ago
I thought you were making a stand-alone jar so your installed Java should not matter in that case.
Lloyd_159
Lloyd_159OPβ€’2mo ago
oh okay lol so sry for my confusion, but which JRE do I have to use and how can I check that I have the right one? (I thought you meant just using the JRE which I have installed or smth like that)
Madjosz
Madjoszβ€’2mo ago
Can you show how you create the jar?
dan1st
dan1stβ€’2mo ago
I think jarfix should allow configuring the Java version/JDK installation you are using?
Lloyd_159
Lloyd_159OPβ€’2mo ago
I include my main class in my pom.xml in maven:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
then I going to File -> project strucutre -> artifacts -> add -> JAR -> from modules with dependencies in that menu I choose a namen and the same main class as the one from the pom.xml file and select "extract to the target JAR", click ok and then apply afterwards I go to Build -> Build Artifacts -> select the artifact I just created and click Build the file appears in my out folder when I run it using cmd as descripted in the original message it works and the gui pops up, but with a double click the error appears no clue how to do that, jarfix it never gave me that option
Madjosz
Madjoszβ€’2mo ago
Double click might not use the -jar in the command.
Lloyd_159
Lloyd_159OPβ€’2mo ago
do you know how I can fix that?
dan1st
dan1stβ€’2mo ago
that's what jarfix takes care of Can you show the output of jarfix?
Lloyd_159
Lloyd_159OPβ€’2mo ago
wait wdym? like when I execute jarfix?
dan1st
dan1stβ€’2mo ago
What does jarfix show when you run it? yeah
Lloyd_159
Lloyd_159OPβ€’2mo ago
uh it's in German
dan1st
dan1stβ€’2mo ago
it's fine
Lloyd_159
Lloyd_159OPβ€’2mo ago
No description
dan1st
dan1stβ€’2mo ago
ok yeah that's a JDK 8 JRE try running jarfix /k and are you logged in as an administrator?
Lloyd_159
Lloyd_159OPβ€’2mo ago
does it matter where I do it? yes
dan1st
dan1stβ€’2mo ago
?
Lloyd_159
Lloyd_159OPβ€’2mo ago
like in which directory because cmd doesn't know the command "jarfix"
dan1st
dan1stβ€’2mo ago
you need to use the right location for jarfix either run it in the directory you installed jarfix in
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay
dan1st
dan1stβ€’2mo ago
or use something like C:\path\to\jarfix.exe /k
Lloyd_159
Lloyd_159OPβ€’2mo ago
well it executed but nothing happened wait nvm jarfix was executed and asked me for permission but nothing else
dan1st
dan1stβ€’2mo ago
no output? not even where you ran it?
Lloyd_159
Lloyd_159OPβ€’2mo ago
I just opened cmd and ran C:\Users\username\Downloads\jarfix.exe /k no output tho
dan1st
dan1stβ€’2mo ago
also no output inside cmd?
Lloyd_159
Lloyd_159OPβ€’2mo ago
nope nothing
dan1st
dan1stβ€’2mo ago
I assume the JAR still doesn't work?
Lloyd_159
Lloyd_159OPβ€’2mo ago
it doesn't
dan1st
dan1stβ€’2mo ago
What's the output of C:\Users\username\Downloads\jarfix.exe without /k?
Lloyd_159
Lloyd_159OPβ€’2mo ago
also no output and jarfix also asks me to allow changes to my pc
dan1st
dan1stβ€’2mo ago
and nothing if you allow it?
Lloyd_159
Lloyd_159OPβ€’2mo ago
no, nothing no gui which opens or output, and also no output in cmd
dan1st
dan1stβ€’2mo ago
adding /u doesn't change anything either?
Lloyd_159
Lloyd_159OPβ€’2mo ago
no change
dan1st
dan1stβ€’2mo ago
Can you try running jarfix from an administrator cmd? i.e. use the "run as administrator" option with cmd
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay no change except jarfix doesn't ask me to allow changes
dan1st
dan1stβ€’2mo ago
and double-clicking jarfix shows the output?
Lloyd_159
Lloyd_159OPβ€’2mo ago
wait no it doesn't now imma restart my device ig?
dan1st
dan1stβ€’2mo ago
Then how did you get to that?
Lloyd_159
Lloyd_159OPβ€’2mo ago
it appeared when running jarfix via double click, but no it doesn't
dan1st
dan1stβ€’2mo ago
maybe it's still open?
Lloyd_159
Lloyd_159OPβ€’2mo ago
back I don't think it was but now I'm not too sure lol anyways I'll run the command again
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay now it has a different window
No description
dan1st
dan1stβ€’2mo ago
that's good
Lloyd_159
Lloyd_159OPβ€’2mo ago
should I try to run my .jar again?
dan1st
dan1stβ€’2mo ago
yes Now it configured to use JDK 20
Lloyd_159
Lloyd_159OPβ€’2mo ago
oh yes it works nice, thanks xD
JavaBot
JavaBotβ€’2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Lloyd_159
Lloyd_159OPβ€’2mo ago
so whenever I need to update jarfix I can just run it with /k?
dan1st
dan1stβ€’2mo ago
/k just means it tries to detect a JDK so it skips JREs
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay got it, thx
dan1st
dan1stβ€’2mo ago
it's also possible to specify a specific JDK
Lloyd_159
Lloyd_159OPβ€’2mo ago
like by just adding the exact jdk instead of /k?
dan1st
dan1stβ€’2mo ago
no you'd need to run it with /x which generates an ini file to configure it - if you edit that file, you can change which JDK jarfix uses
dan1st
dan1stβ€’2mo ago
Jarfix
Jarfix fixes the jar file association on Windows
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay I'll definitely safe this xD
dan1st
dan1stβ€’2mo ago
alternatively you can just select to associate the .jar extension when installing a JDK At least with Adoptium/Temurin (the JDK I'd usually use), that works well
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay thanks again, thank you very much for your help <3
dan1st
dan1stβ€’2mo ago
If you install the JDK from Oracle, idk whether that option is present
Lloyd_159
Lloyd_159OPβ€’2mo ago
I don't think so, but they seem to have their own download page
Lloyd_159
Lloyd_159OPβ€’2mo ago
yea that's where I've been rn :P
dan1st
dan1stβ€’2mo ago
I'd install Java from there because - It works well/doesn't cause issues - It's TCK certified (OracleJDK and most other major JDKs are as well) and production-ready - It's actually an Open Source version and you can use it commercially forever without paying for it - You have the option to enable/disable these things - some other things maybe
Lloyd_159
Lloyd_159OPβ€’2mo ago
I've been using Amazon Corretto bc it's the intellij preset, but if you recommend this one I'll definitely change I think lol at least before creating the next project
dan1st
dan1stβ€’2mo ago
Coretto is fine as well But yeah if you install the JDK from inside IntelliJ, it won't ask you to associate the .jar extension
Lloyd_159
Lloyd_159OPβ€’2mo ago
I guess I'll still have a look when I create my next project, bc I know nothing abt the jdk I use actually πŸ˜Άβ€πŸŒ«οΈ
dan1st
dan1stβ€’2mo ago
The JDK is the Java installation you are using to develop your application newer JDK means you can use more Java features
Lloyd_159
Lloyd_159OPβ€’2mo ago
yes I know that, i meant I don't know nothing about the difference between the distributions like corretto, adoptium, opendjk etc
dan1st
dan1stβ€’2mo ago
it's also possible to use a new JDK to write applications for older Java versions (if an application is written for some Java version, it can normally be used with all newer Java versions) but then you cannot use the new features of the new version these are just different vendors/organizations
Lloyd_159
Lloyd_159OPβ€’2mo ago
okay gotcha
dan1st
dan1stβ€’2mo ago
or flavours in some cases (there are cases where some vendor has multiple flavours with some difference)
Lloyd_159
Lloyd_159OPβ€’2mo ago
but the java features they provide are the same, right? like the code of openJDK 21 ir the same as adoptium 21 for example
dan1st
dan1stβ€’2mo ago
there's also https://whichjdk.com/ if you are interested
whichjdk.com
Which Version of JDK Should I Use?
Which Version of JDK Should I Use?
dan1st
dan1stβ€’2mo ago
yes
Lloyd_159
Lloyd_159OPβ€’2mo ago
oh okay this is useful
dan1st
dan1stβ€’2mo ago
though there isn't really a single "OpenJDK" using that name to refer to a specific JDK isn't really correct
Lloyd_159
Lloyd_159OPβ€’2mo ago
like there are different organisations which call their jdk like that?
dan1st
dan1stβ€’2mo ago
both Adoptium/Temurin are openjdk/jdk distributions just like Coretto
Lloyd_159
Lloyd_159OPβ€’2mo ago
oh okay that makes sense
Madjosz
Madjoszβ€’2mo ago
I just change my registry entries directly.
dan1st
dan1stβ€’2mo ago
- OpenJDK is https://openjdk.org and https://github.com/openjdk - you can't download a JDK from there (only build it yourself) - Oracle is the company doing most of the work behind Java and provides OracleJDK (commercial) - they also make GraalVM - The Eclipse Foundation (a not for profit organization with many member companies) provides Temurin (Adoptium is their project name) - Amazon makes Corretto - Azul makes Zulu and Azul Prime - ...
GitHub
OpenJDK
OpenJDK has 124 repositories available. Follow their code on GitHub.
dan1st
dan1stβ€’2mo ago
I just don't use Windows lol and even on Windows, the Temurin msi installer sets it up properly
Lloyd_159
Lloyd_159OPβ€’2mo ago
oooh okay, that made things more clear now
dan1st
dan1stβ€’2mo ago
the main difference is commercial support and licensing there are also some performance differences and some non-language (e.g. tooling, GC, etc) features in some cases
Lloyd_159
Lloyd_159OPβ€’2mo ago
yea okay
JavaBot
JavaBotβ€’2mo ago
Post Closed
This post has been closed by <@765577368030937129>.

Did you find this page helpful?