Luis_V Luis_V - 5 months ago 16x
Java Question

How to run java graphic programs without IDE?

I'm trying to create a java program that uses the graphic default library
I managed to find this example of how to generate a simple window from the Oracle tutorial page:

package components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* requires no other files. */
public class FrameDemo {
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");

JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

The code compiles when "javac" is used from the terminal.
But at the moment of running it as "java FrameDemo", it's shown the next line:

Error: Could not find or load main class FrameDemo

My questions are:

  • In wich directory can I see the available libraries?

  • Why, at the moment of compiling the file, it creats 2 .class
    archives (FrameDemo.class and FrameDemo$1.class)

  • What do I need to do in order to use the graphic lib? Do I need to use a linker or install something?

  • Could you recommend me any other external 2D graphic library?

My java version is:

java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

Please tell me I can make graphic programs without using a IDE.


Your problem is that your code is in a package.

Remove the line:

package components;

and then do

java FrameDemo

and you will get your working result.

Alternatively, you can leave the package and compile using -d. Example (compile to current directory):

javac -d .

This creates a directory called 'components'. From your original working directory (same level as components folder), you can then run

java components.FrameDemo

To answer the questions I can: Why 2 .class archives? Take a look at what are the $1 in class file?

You have an 'anonymous inner class' in main when you instantiate the new Runnable.

You don't need to do anything to use graphic lib, it is part of java.