92

Consider:

public class LoginCumReg implements ActionListener, KeyListener {

    private JFrame form;
    private JTextField txtunm;
    private JTextField txtnm;
    private JTextField txteml;
    private JButton cmdcreate;
    private JPasswordField txtpass;
    private JPasswordField txtpassreg;
    private JButton cmdok;
    private JLabel lblunm;
    private JLabel lblpass;
    private JLabel lbleml;
    private JLabel lblpassreg;
    private JLabel lblnm;
    private JPanel panel_1;

    public LoginCumReg() {
        // Construct components
        form = new JFrame("Sign Up");
        form.getContentPane().setFont(
                new Font("Plantagenet Cherokee", Font.BOLD, 18));
        txtunm = new JTextField(5);
        txtunm.addKeyListener(this);
        txtunm.setBounds(637, 55, 100, 25);
        txtnm = new JTextField(5);
        txtnm.setBounds(637, 228, 100, 25);
        txteml = new JTextField(5);
        txteml.setBounds(637, 264, 100, 25);
        cmdcreate = new JButton("Create Account");
        cmdcreate.setBounds(527, 350, 188, 25);
        txtpass = new JPasswordField(5);
        txtpass.setBounds(637, 91, 100, 25);
        txtpassreg = new JPasswordField(5);
        txtpassreg.setBounds(637, 300, 100, 25);
        cmdok = new JButton("OK");
        cmdok.setBounds(527, 139, 100, 25);
        lblunm = new JLabel("UserName");
        lblunm.setBounds(527, 55, 73, 25);
        lblpass = new JLabel("Password");
        lblpass.setBounds(527, 91, 100, 25);
        lbleml = new JLabel("Email ID");
        lbleml.setBounds(527, 264, 100, 25);
        lblpassreg = new JLabel("Password");
        lblpassreg.setBounds(527, 300, 100, 25);
        lblnm = new JLabel("Full Name");
        lblnm.setBounds(527, 228, 100, 25);

        // form.setResizable(false);

        // Adjust size and set layout
        form.setPreferredSize(new Dimension(800, 450));

        form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        form.getContentPane().setLayout(null);

        // Add components
        form.getContentPane().add(txtunm);
        form.getContentPane().add(txtnm);
        form.getContentPane().add(txteml);
        form.getContentPane().add(cmdcreate);
        form.getContentPane().add(txtpass);
        form.getContentPane().add(txtpassreg);
        form.getContentPane().add(cmdok);
        form.getContentPane().add(lblunm);
        form.getContentPane().add(lblpass);
        form.getContentPane().add(lbleml);
        form.getContentPane().add(lblpassreg);
        form.getContentPane().add(lblnm);

        cmdcreate.setActionCommand("Create Account");
        cmdcreate.addActionListener(this);
        cmdok.setActionCommand("Login");

        JPanel panel = new JPanel();
        panel.setBounds(502, 29, 254, 154);
        panel.setFont(new Font("Palatino Linotype", Font.PLAIN, 14));
        panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED,
                null, null), "LOGIN", TitledBorder.LEADING, TitledBorder.TOP,
                null, null));
        panel.setBackground(SystemColor.controlHighlight);
        form.getContentPane().add(panel);

        panel_1 = new JPanel();
        panel_1.setBounds(500, 204, 254, 186);
        panel_1.setFont(new Font("Palatino Linotype", Font.PLAIN, 14));
        panel_1.setBorder(new TitledBorder(new EtchedBorder(
                EtchedBorder.RAISED, null, null), "SIGN UP",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_1.setBackground(SystemColor.controlHighlight);
        form.getContentPane().add(panel_1);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(91, 139, 286, 101);
        lblNewLabel.setIcon(new ImageIcon(
                "C:\\Users\\JAsh\\Desktop\\Projects\\JAsh.png"));
        form.getContentPane().add(lblNewLabel);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(77, 118, 315, 145);
        panel_2.setBorder(new EtchedBorder(EtchedBorder.RAISED,
                SystemColor.activeCaptionBorder, SystemColor.menu));
        panel_2.setBackground(SystemColor.controlHighlight);
        form.getContentPane().add(panel_2);
        cmdok.addActionListener(this);

        form.setResizable(false);
        form.pack();
        form.setVisible(true);
        System.out.println("const..");
    }

    public static void main(String[] args) {
        new LoginCumReg();
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub

        if (ae.getActionCommand().equals("Create Account")) {
            new Registration(txtnm.getText(), txteml.getText(), new String(
                    txtpassreg.getPassword()));
            form.setVisible(false);
        }

        if (ae.getActionCommand().equals("Login")) {
            try {
                if (txtunm.getText().equalsIgnoreCase("admin")
                        && new String(txtpass.getPassword()).equals("admin")) {
                    form.setVisible(false);
                    new Admin();
                } else {
                    DataBase db = new DataBase();
                    db.connect();
                    String sql = "SELECT * FROM LOGIN WHERE USERNAME='"
                            + txtunm.getText() + "'";
                    ResultSet rs = db.getDDL(sql);
                    boolean found = false;
                    while (rs.next()) {
                        if (rs.getString("Password").equals(
                                new String(txtpass.getPassword()))) {
                            found = true;
                            form.setVisible(false);
                            String name = rs.getString("FullName");
                            Session.startSession(txtunm.getText(), name);
                            new FacultyWelcome();
                        }
                    }
                    if (!found)
                        JOptionPane.showMessageDialog(new JFrame(),
                                "Invalid Credentials");
                    db.close();
                }
            }
            catch (Exception e) {
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent arg0) {

        // TODO Auto-generated method stub
        // JOptionPane.showMessageDialog(new JFrame(), arg0.getKeyChar() + "-" + arg0.getKeyCode());
    }
}

The above code works perfectly on my friend's laptop and throws this error on my laptop. I have running the code on Eclipse v4.2 (Juno) using Java 7.

I have also tested and run a lot of Java programs on my laptop all works well. This is the only program which gives such an error.

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jash Parekh
  • 1,097
  • 2
  • 10
  • 15

42 Answers42

72

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!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Professor
  • 799
  • 6
  • 9
  • This worked for me but I left Java 8 on the system and I had to change the properties of the project to use Java 12. No more JNI error. – nicomp Sep 30 '19 at 14:47
  • 3
    You do not need to uninstall `java 8`, just configure your java versions on your system `sudo update-alternatives --config javac; sudo update-alternatives --config java ` – Nicole Finnie May 31 '20 at 09:12
  • @NicoleFinnie Too complicated and took much work. – NoName Jan 14 '22 at 16:44
  • *"I have always believed that JRE and JDK are different application, but it turns out things have changed."* - Nope. Nothing has changed. A JDK has always included a JRE. All the way back to the Java 1.1 days. JREs and JDKs are different **distributions** not different applications. – Stephen C May 16 '22 at 01:35
54

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Positive Navid
  • 2,481
  • 2
  • 27
  • 41
  • 3
    Your **Short Answer** Helped. Only I need to write a new application name and select my main function containing class. It worked like charm. – Jaffer Wilson Feb 04 '17 at 14:12
  • How do you resolve the error if the java file is a part of an android project on android studio – ubuntu_noob Jun 02 '18 at 10:33
  • 1
    On the project level, I referenced JRE library to 1.8.xx and worked for me.Thanks – Shetty's Sep 20 '19 at 10:30
  • Just worked for me when I deleted all project configuration files. Which is equivalent to create a new project. – axell-brendow Sep 21 '19 at 15:26
  • 1
    it seems that this occurs when eclipse itself uses different JDK from current Java Project, after checked -Djava.library.path of Java process started by Eclipse ant launcher, dll path of Eclipse JRE is placed first, Ant's one (used by launching configuration) is placed last, so I think this is a bug of Eclipse Ant launcher plugin. – Daniel Yang Jun 04 '20 at 04:06
20

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.

c0der
  • 18,467
  • 6
  • 33
  • 65
  • How do you fix this? – Atnas Apr 04 '20 at 20:29
  • 2
    @Atnas I changed the compiler jdk compliance. In Eclipse its under project properties > java compiler > jdk compliance – c0der Apr 05 '20 at 04:40
  • 1
    @c0der this solved my problem thanks! – Assegd Oct 05 '21 at 06:02
  • Please review *[Should we edit a question to transcribe code from an image to text?](https://meta.stackoverflow.com/questions/415040)* and *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and take the appropriate [action](https://stackoverflow.com/posts/53536389/edit) (it covers answers as well). – Peter Mortensen May 23 '22 at 20:10
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. :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user8472395
  • 139
  • 1
  • 2
13

I solved it by changing my 'Compiler compliance level'. In Eclipse:

Menu WindowPreferencesJavaCompiler.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
juancarlosun
  • 153
  • 1
  • 7
  • 1
    This worked for me. The "Run As' fix listed above didn't help. – nicomp Mar 25 '20 at 14:52
  • 1
    Thanks! The issue for me was that I previously had Java 12 installed and then went to Java 1.8, but Eclipse didn't pick up on the Java version change for the compliance level. – i01573 Oct 25 '21 at 15:46
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
DennisLi
  • 3,915
  • 6
  • 30
  • 66
CoDeSigns
  • 141
  • 1
  • 2
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salvador Vigo
  • 397
  • 4
  • 16
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DvixExtract
  • 1,275
  • 15
  • 25
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Miguel Ormita
  • 171
  • 2
  • 3
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Devendra
  • 125
  • 2
  • 10
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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
erank
  • 501
  • 4
  • 3
3

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jasmohan
  • 237
  • 3
  • 5
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
b.s
  • 2,409
  • 2
  • 16
  • 26
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.

2

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.

dbakiu
  • 238
  • 4
  • 8
  • What ***exact*** error message did you get? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/56866792/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 23 '22 at 20:14
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
H S Rathore
  • 1,954
  • 2
  • 15
  • 20
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spiderman
  • 10,892
  • 12
  • 50
  • 84
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

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

This could solve your problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Forte
  • 11
  • 1
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Chris C
  • 1,012
  • 2
  • 12
  • 19
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dan
  • 741
  • 9
  • 14
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ioannis Tsiokos
  • 835
  • 1
  • 12
  • 14
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I faced the same issue in NetBeans 8.0.2. Clean and Build Project solved this problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michu93
  • 5,058
  • 7
  • 47
  • 80
0

You can try: right click on the project and then click clean. After this run the project.

It works for me.

lupin
  • 173
  • 5
  • 19
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Community
  • 1
  • 1
ntucci
  • 63
  • 1
  • 8
  • The last sentence is incomprehensible. Can you [fix](https://stackoverflow.com/posts/52519542/edit) it? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen May 23 '22 at 20:05
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kiran
  • 77
  • 1
  • 11
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
java-addict301
  • 3,220
  • 2
  • 25
  • 37
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 35
  • 5
0

Using Gradle, the following also worked for me:

Package Explorer → right-click project → GradleRefresh Gradle Project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
java-addict301
  • 3,220
  • 2
  • 25
  • 37
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chandan Kumar
  • 313
  • 4
  • 9
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 56
  • 1
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siddu
  • 77
  • 1
  • 8
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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Palmeta
  • 61
  • 5
0

I run into a similar issue but in the Android SDK command line tool:

Error: A JNI error has occurred, please check your installation and try again in SDK Manager.

Solution: Cheak your JDK version, SDK manager needs JDK 11 at least to work properly.

PS: Remember to update the system variable's path to the new location. If it wasn't there, it would be better if you add it, so you can run SDKManger and other tools directly from the command line.

Kossei Necira
  • 41
  • 1
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 11 '23 at 12:14
-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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131