.jar file do not run

Answered

Hi JetBrains,

I've started learning Java on Hyperskill, and I'm pretty new to coding and also to Intellij.

While following the tutorials I made my own version of the TicTacToe Game that is now completed. Everything's working inside IDEA, and so I wanted to have it a standalone game.

I followed the tutorial https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html?keymap=primary_windows in order to get a .jar file, but whenever I try to run it nothing happens. No error message, no popup or something.

So I complete uninstalled Java and everything, and reinstalled it, cleared the current .jar file through IDEA, complied a new one but still the file does nothing at all.

Maybe I did something wrong?

Cheers
Snitgit

 

0
17 comments

Does it work as expected when you run it from sources by executing main class in Editor and only running from jar does not work?

Please attach screenshot or Run/Debug Configuration and result of it's running or upload a full sample project to check (you can use https://uploads.jetbrains.com/ for uploading). Thanks.

You may also try first create a simple hello world program and run it from jar just to see how it works for you on a simple app.

0

Hi Andrey Dernov,

thank you for your feedback. 

As suggested I just made a quick Hello World, but it has got the very same issue as my game - the .jar just does nothing.
What further info do you need?

 

Cheers
Snitgit

 

0

Can you show screenshot of your Run/Debug Configuration that you launch to run the jar? And the output you get when try to run it. Thanks.

0

Not quiet sure where to find these info. I just double click the .jar file in the out-folder.
I just hit the green bug-button top right, hope this is what you are looking for

0

Thank you. The screenshot shows that the application has run and the "Hello World" is printed - as expected. Please clarify what is not working as you expect it should? Thanks.

0

There is the .jar file located on my hard drive F:\java\Git\helloworld\out\artifacts\helloworld_jar\helloworld.jar
When I double click the file in order to run my code (outside of IDEA) the Windows console should open, and Hello World should be printed in the console.
The same should happen with the TicTacToe game, I want to send it to my friends to use it.

 

0

To run the jar you should use java -jar <jar name>.jar command in console, please see https://stackoverflow.com/a/1619204/2000323

To run it in IntelliJ IDEA you use the Jar Run/Debug Configuration, please see Run JAR artifacts.

0

Thank you, I tried the java -jar <jar name>.jar method, and now I'm facing some other difficulties.

Could you help me solving this too, please?

 

PS F:\java\Git\helloworld\out\artifacts\helloworld_jar> java -jar helloworld.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: helloworld has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

0

You compiled the jar classes for JDK 11 version but are trying to run it by JDK 8. You should either use JDK 11 to run it or compile the jar with target (bytecode) level of 8 (see how to configure it in IDE Java Compiler settings). If you are using Maven/gradle target bytecode level must be confogured in Maven pom.xml / Gradle build.gradle files.

0

Thanks again Andrey, may I ask how to run the jar through Windows CMD in Java 11? Or rather where to download Java 11, since I already have the jdk 11 but I can't find Java in any version higher than 8 for download?

0

>how to run the jar through Windows CMD in Java 11

You need to have <JDK 11 home>/bin directory in the PATH OS environment variable see e.g. how to add it from command line or in Windows Settings.

or include the full path to java executable form JDK 11 installation, e.g. <full path to JDK 11>\bin\java.exe -jar helloworld.jar

To download JDK 11 you can using IntelliJ IDEA when adding new JDK to IDE or download it form Oracle site.

0

Hi Andrey Dernov... i reach this blog cos' im having troubles generating the jar file.

I just made the most simple code to try this https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html?keymap=primary_windows

im getting this on console: 

Error: Could not find or load main class TestPrueba.jar
Caused by: java.lang.ClassNotFoundException: TestPrueba.jar

 

0

Hi Lucasmuscolo,

does your code contain the main method?

To be precice:
public class Main {
public static void main(String[] args) {
}
}

0

yes. it cointains de main method...

0
Avatar
Yaroslav Bedrov

Is it possible to share your project sample?

0

I posted the same question on stackOverflow and some one give me this solution.

I was creating a .bat file to open the jar using this line " java myProgram.jar" istead of " java -jar myProgram.jar"

Adding "-jar" it works...

There is a way to solve this in IntelliJ so i can execute myProgram from the jar itself???

0
Avatar
Yaroslav Bedrov

If you run JAR file then it's necessary to use "-jar" flag. And it runs your Main class just like if you run it from IDE. 

0

Please sign in to leave a comment.