1

I'm following along with this tutorial, and I got a problem early in the video (at approximately 7:45). I'm trying to create a basic Java program that will launch a window, however, I can't seem to import JFrame.

I've looked for other solutions on Stack Overflow, but I haven't found one that works for me.

Here is the code I've written:

import javax.swing.JFrame;

public class App {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Hello World");

        frame.setVisible(true);

    }

}

I'm using Eclipse version 4.12.0 on a Macbook Pro (13-inch, Mid 2012) running macOS Mojave version 10.14.5

Expected result: A window opens when I run the program, and when I close the window, the program ends.

Actual result: No window is created and I get this error message:

Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: /Users/username/eclipse-workspace/Swing1/bin
Caused by: java.lang.module.InvalidModuleDescriptorException: App.class found in top-level directory (unnamed package not allowed in module)
Peter-Andre
  • 33
  • 1
  • 6

3 Answers3

3

If you have a module-info.java file, put this in the module:

requires java.desktop;
1

If you have created the java app with eclipse your fault is the package.

With eclipse, I created a java app and this is the result

enter image description here

This code fixed your foult

package demo;

import java.awt.Dimension;

import javax.swing.JFrame;

public class App {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Hello World");

        frame.setLocationRelativeTo(null);
        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);

    }

}

The reference for understand the package

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
0

I had the same issue. I did similar code in Eclipse. I got the error The type javax.swing.JFrame is not accessible on the side of import javax.swing.JFrame; The solution is: Delete the line of import javax.swing.JFrame; And then, inside your body of your code inside your main, with your mouse, hover over JFrame keyword, and Eclipse will offer some auto-completion suggestion. Select import 'JFrame' (javax.swing) This will bring the required import automatically. It is a kind of shortcut. In order to avoid these type of errors: Never type manually, get the imports AND methods, for example setVisible by autofilling. For instance, type frame.setV and again Eclipse will suggest the completion ... select from there. I do not know why, but this is what happened in my case.