score:52

Accepted answer

solution 1: right-click on the class that contains the main method. click on "run as". click on "java application". the keyboard shortcut is: shift + alt + x, j (while holding shift and alt, press x; then release shift and alt and press j).

solution 2: as professor mentioned, please make sure that java -version and javac -version are the same.

note: solution 1 is a quick fix and worked for me and a few other people. it may or may not work for you. i think solution 2 is the proper way to solve it.

score:-1

in netbeans 8.0.2:

  1. right-click on your package.
  2. select "properties".
  3. go to "run" option.
  4. select the main class by browsing your class name.
  5. click the ok button.

score:0

edit the ".classpath" and include the below tag:

<classpathentry kind="con" path="org.testng.testng_container"/>

this could solve your problem.

score:0

in my project i had changed my maven pom file and removed the entry for maven-jar-plugin.

when you build a jar file whose purpose is to be executable, you need to include this so that certain entries get written into the manifest. i opened the old project, copied that entry (with some modifications for project name) and it worked.

score:0

i think this happens because the name of your class is the same name of another class in the jdk. help eclipse to determine which one you are trying to run by selecting the package your class is in. go to run configuration, select the project and the main class.

when you press the search button to select the main class, you will find options that have the same name but different package, select your class.

score:0

my issue came from trying to disable the maven nature from my project and then deleting the pom.xml file. this is bad, very bad, don't do this. if you do, change it back to a maven project and hopefully you can either restore your old pom.xml file or create a new one. as soon as i did that, it worked again.

edit: i recently ran across this problem again and this time it was because i had duplicate libraries in my classpath. the one maven added (which started with /m2_repo/.... and then one i had added (which i should not have done). even though eclipse was reporting that it could not find the class, the actual problem was that i had given it two libraries with the same class and it didn't know which one to use. once i removed the second reference, it worked.

score:0

there was no information in my console so that sent me searching for additional solutions and found these - unique to the solutions presented here. i encountered this with eclipse v4.7 (oxygen) trying to run an old ant build on a project.

cause 1

i had configured eclipse to use an external ant install which was version 1.10.2 which apparently had classes in it that were compiled with jdk 9. in eclipse i got the jni error described above (running the ant build at the command line gave me the renowned 'unsupported major.minor version' error - the java i was using on the system was jdk 8).

the solution was to roll back to the embedded eclipse version of ant being 1.10.1. i verified this as the correct solution by downloading ant 1.10.1 separately and reconfiguring eclipse to use the new 1.10.1 externally and it still worked.

cause 2

this can also happen when you have the ant runtime settings configured incorrectly in eclipse's preferences. depending on the version of ant you're running you will need to add the tools.jar file from the appropriate jdk to the classpath used for the ant runtime (home entries). more specifically, without a proper configuration, eclipse will complain when launching an ant target that the jre version is less than a particular required version.

essentially, 'proper configuration' means aligning each of the configuration items in eclipse for running ant so that they all work together. this involves the ant runtime home entry (must point to an ant version that is compatible with your chosen jdk -- you can't run ant with jdk 8 when it was compiled against jdk 9); specifying the tools.jar that belongs to the jdk you want to run ant with in the ant runtime settings; and lastly setting the jre environment of your build script to the jdk you want to run ant with in the external tools configuration.

all three of these settings need to agree to avoid the error described above. you'll also need to consider the attributes used in your javac tag to ensure the jdk you're using is capable of executing as you've directed (i.e., jdk 7 can't compile code using source and target version 8).

moreover

if you're really just trying to run an ant build script to compile code to an older jdk (e.g., less than 8 for oxygen), this article helped gain access to run ant against an older jdk. there are ant plugin replacements for a handful of versions of eclipse, the instructions are brief and getting the correct plugin version for your particular eclipse is important.

or more simply you can use this very good solution to do your legacy compile which doesn't require replacing your eclipse plugin, but instead changing the javac tag in your build script (while using the latest jdk).

score:0

i have been running into the same error when i adding the following maven dependency in my project:

<artifactid>aws-encryption-sdk-java</artifactid>

the error came up only when i ran the shade jar file produced by maven-shade-plugin. i was able to overcome the error by using the jar file produced by maven-assembly-plugin.

score:0

all is not working for me, but once i delete the ".metadata" and ".recommenders" of the workspace, delete the "bin" and "setting" folders, and then after i reimport the same project, then it works for me.

score:0

i faced the same issue in netbeans 8.0.2. clean and build project solved this problem.

score:0

you can try: right click on the project and then click clean. after this run the project.

it works for me.

score:0

the programs which has been written on one version of jdk won't support the jni platform of another version of the jdk.

if in case we are using jdk 10 and jdk 8 with eclipse configured for jdk 10 and code written on jdk 10. now, i don't want to use jdk 10 and started using jdk 8 as jvm and tried to run the code which is written on jdk 10, so eclipse will threw an error of jni.

so, to come out of this error, please add the current jvm path to eclipse.ini file. after this, copy the written code to the clipboard. delete the project in eclipse, create a new project, check in the copied code, and run.

score:0

if the problem occurs while launching ant, check your ant home: it must point to the same eclipse folder you are running.

it happened to me while i reinstalled a new eclipse version and deleted the previous eclipse folder while keeping the previous ant home: ant simply did not find any java library.

in this case the reason is not a bad jdk version.

score:0

i have faced the same issue due to the packaged name being "java". after renaming the package, it was no longer throwing an error.

score:0

in my case, eclipse was giving me this error, but i was getting classnotfoundexception on javax.servlet.http.httpservlet in the logs.

in my case, i was trying to start a spring boot application with exclude module: 'spring-boot-starter-tomcat' in my build.gradle file (since i'm deploying on wildfly).

the solution was to comment this exclude out. the eclipse jni error was very misleading and i would suggest fixing this.

score:0

i had the same issue. i had imported a newer version of a json library (i deleted the old one, but i did not remove it from the build path).

i resolved it by removing the reference to the old one from the build path.

score:0

using gradle, the following also worked for me:

package explorer → right-click project → gradlerefresh gradle project.

score:0

when we get this error while running our java application, check if we have some class name or package names starting with ”java.”. if so, change the package name.

this is happening because your package name is colliding with java system file names.

score:0

error/issue:

while trying to run my application as a java application or as a spring boot application, i am getting below error

a jni error has occurred, please check your installation and try again. a java exception has occurred.

as well as getting this error in the console:

exception in thread "main" java.lang.unsupportedclassversionerror: com/example/demo/iocapplication 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`

what is the root cause?

the reason for this error is that

  • the code is getting compiled on a higher version. in my case, it is version 11... and
  • the code is getting executed on a lower version. in my case it is version 8.
  • i.e., java (jdk) compiled code on 11, whereas the jre version is 8.

where did i configure jdk 11 and jre 8?

  • i am using springtoolsuite (sts). when i created the project, the jre system library was in the build path, which got added as the default jre library version of 8 (1.8). but when i go to the project → propertiesjava compilercompiler compliance level, it is by default having a selected version of 11

solution:

the project → propertiesjava compilercompiler compliance level

  • set the compiler compliance level to 1.8 that's it. now you are compiling your code in 1.8 and executing the compiled code with a jre 1.8.

the accepted answer did not resolve the issue, hence i am posting what worked for me.

score:0

this is a core problem. try javac --version and java --version. you will see a mismatch or inconsistency. try to set path variables correctly and you will not see any errors.

for eclipse, right-click on the project → properties. then go to project facets and set the jdk to the desired version. then go to run-times and set jre to the same version. you should be good.

score:0

i'm was trying to run my jar file (without external jar files) adding libraries at runtime java -cp "..." -jar mycode.jar, but when you specify -jar then the -cp parameter will be ignored.

so you can include your jar file in the classpath and then call your main class:

java -cp "mycode.jar:/path/to/jars/*" foo.bar.mymainclass

run a jar file from the command line and specify classpath

score:0

while running java and javafx applications in eclipse, i have faced the same problem. but i got a simple solution. i have removed jdk 1.8 from the build path and added the same jdk 1.8 to the build path. it works now.

score:0

i faced the same problem when i checked-out a project from a git repository.

to resolve this, i added these properties in the pom.xml file to tell which java version to use (we are using java 1.8, but you can use any version number as per your requirement).

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

score:0

this problem probably occurs because the jdk versions are inconsistent, and the jar package will have signature files, which do not affect the logic of the program, but they will cause the test demo to fail. therefore, you need to delete the signature related files in the jar package.

delete the .dsa and .sf files under meta-inf in the generated jar file.

score:1

i faced a similar problem and then got the solution in the package name.

i kept the package name as java.basics. in the console i got a hint for that as it clearly said "prohibited package name". so i changed the package name and it worked.

score:1

possible solution

if none of these solutions worked for you then try this... i finally got mine working by deleting the two folders ".metadata" and ".recommenders" in the workspace folder.

you can find your workspace folder path by clicking on menu fileswitch workspaceother in the eclipse application's toolbar.

now i am a complete noob crying my way through an software engineering degree at san jose state university. so i am just sharing what worked for me without knowing much knowledge of why exactly.

i do not know what harm or headaches, or even if both aforementioned folders needed to be deleted. but in the end, it worked for me and it seems the application just recreates fresh versions of those folders anyway.

score:1

i experienced the same issue, however in my case it was due to the configuration settings in intellij idea.

even though the project sdk (file -> project structure) was set to java 11, the jre in the run/debug configurations (run -> edit configurations) was set to java 8. after changing that to java 11, it worked like a charm.

score:1

just check small step...

javac -version

javac 17

java -version

java version "17" 2021-09-14 lts

both javac and java versions are the same. if different, you will get this error.

score:2

check your console. it says java.lang.securityexception issue. change your 'package name'. i changed my package name from 'java.assessment' to 'assesment' and it worked for me. if somebody knows the root cause , let me know please.

score:2

i have found that adding these lines of code to the pom.xml file of a maven project solves similar issues for me:

<build>
    <plugins>
        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-jar-plugin</artifactid>
            <configuration>
                <archive>
                    <manifest>
                        <mainclass>com.packagename.mainclassname</mainclass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-shade-plugin</artifactid>
            <version>2.3</version>
            <configuration>
                <createdependencyreducedpom>false</createdependencyreducedpom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

score:3

i was facing the same issue and in my case the compiler compliance level was selected as 14 in my eclipse settings. in order to fix it, i changed it to the version of jdk on my machine i.e 1.8 and recompiled the project.

enter image description here

score:4

i also faced the same issue. by looking at the console that's saying

java.lang.securityexception issue.

the solution is: check your package name of your project.

score:4

it can happen if the jdk version is different.

try this with maven:

<properties>
    <jdk.version>1.8</jdk.version>
</properties>

under buildplugins:

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <version>2.3.2</version>
    <configuration>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
    </configuration>

</plugin>

score:5

you can check the console first to see the actual error message and then proceed by looking for answers for that error.

my console shows the error noclassdeffound for the class jcommander.

your error will probably be different from mine. the cause of my error is that the class jcommander is not in my m2 repository, so i just have to add that dependency in my pom.xml file. you can check if all the class dependencies are all declared and added in your current configuration and project build.

noclassdeffounderror

score:6

in my case the issue was caused by the jdk version. to solve this i suggest to go into your pom file and check the <jdk.version>1.7</jdk.version>.

afterwards, check the java version in your cmd (windows) or terminal (os x or linux) by typing: java -version.

compare the version shown in the terminal/command prompt with what you have in your pom file. make sure they are the same. then run your project again.

score:7

i faced a similar problem with a project, in intellij with maven dependencies.

i solved it changing the dependencies scope from provided to compile.

intellij:

menu → fileproject structuremodulesdependencies → *scope (provide to) → compile.

score:10

i had similar issues when using 'java' at the beginning of the package name, e.g. java.jem.pc

check your console output, i was receiving:

exception in thread "main" java.lang.securityexception: prohibited package name: java.jem.pc

score:12

i solved it by changing my 'compiler compliance level'. in eclipse:

menu windowpreferencesjavacompiler.

score:13

error: a jni error has occurred, please check your installation and try again in eclipse

solution: check your package name as it may be colliding with the package name in java. simply change the package name to resolve your issue. :)

score:19

running the jar from command line by:

c:\folder\where\jar\stored>java -jar jarname.jar

i got information which made the problem clearer:

main/main has been compiled by a more recent version of the java runtime

enter image description here

in this case, the jar was compiled with a version newer than jre supports.

score:66

i have been having this problem for a while, but now i have figured it out.

it turns out that java jdk 12 has both the jre and the jdk inside the bin folder of the java 12 jdk. (i have always believed that jre and jdk are different application, but it turns out things have changed.)

my problem was that i had java jdk 12 installed on my computer. at the same time, i had java 8 (jre) installed on my computer. so my computer was getting confused.

with my command prompt environment set up to run java and javac commands:

i typed the follow commands:

java -version

this gave me java 8.

after that, i typed:

javac -version

this gave me java 12.

in other words, my program is getting compiled with java 12 and i am trying to run with java 8.

to solve the problem, i uninstalled java 8 jre from my computer.

i went back to command prompt to check if "java -version" and "javac -version" was returning the same version number, and yes, it was returning java 12.

i tried recompiling my program again, and running it. it worked!!


Related Query

More Query from same tag