LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JAVA : cannot find class JFrame? (https://www.linuxquestions.org/questions/programming-9/java-cannot-find-class-jframe-189108/)

OmniXBro 06-03-2004 12:35 AM

JAVA : cannot find class JFrame?
 
hi everyone :)
i'm a linux newbie who's only installed mandrake 9.2 a few months ago....
the problem is, i'm trying to javac this .java file i made to try to understand java gui application, and i got an error message saying
Cannot locate class "JFrame"

i've
imported javax.swing.*,
java.io.*,
java.awt.*;

and the only class in the file extends JFrame. i tried looking around and heard some stuff about classpath, but i still don't know anything. can anybody help?
thanks :)

keikun_naruchan 06-03-2004 03:39 AM

just a tip
 
I think you need to set your CLASSPATH.. or you could try typing javac while you are in the directory where the binary(executable) is stored.. try looking in BIN directories..

HOPE this helped..

could you look also at my post and help me with JDBC problem.. i used jcreator to program in java and use microsoft access database thru ODBC.. id appreciate any suggestion you could give me.. to find my post just search for JAVA.. the title of my post is JDBC problem......

dave_starsky 06-03-2004 05:51 AM

I think by default that classes like awt and swing are included in the classpath.

Could you post your code?

OmniXBro 06-03-2004 07:23 AM

Code:

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

public class StopGo extends JFrame
{
  private final int d = 100, o=50;
  public void paint (Graphics g)
  {
      boolean condition = (Math.random() >= 0.5);
      if (condition)
        g.setColor(Color.red);
      else
        g.setColor(Color.green);
      g.fillOval(d,d,d,d);
      g.drawString ("Hello world!", 150, 150);
      g.setColor(Color.blue);
      g.drawOval(50, 100, 50, 50);
  }
}

here's the code... the compilation error pointed to the class name with the first mention of JFrame. no compilation errors occured on the import lines though.
any help is greatly appreciated >_< thank you!

DiWi 06-03-2004 07:52 AM

Hi

Using eclipse the following code works
Quote:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Blub extends JFrame
{
private final int d = 100, o=50;
public void paint (Graphics g)
{
boolean condition = (Math.random() >= 0.5);
if (condition)
g.setColor(Color.red);
else
g.setColor(Color.green);
g.fillOval(d,d,d,d);
g.drawString ("Hello world!", 150, 150);
g.setColor(Color.blue);
g.drawOval(50, 100, 50, 50);
}
}
Try using the eclipse SDK http://www.eclipse.org You'll find several Linux ports (GTK/Motif)

Dirk

linux_ub 06-03-2004 08:52 AM

how were u able to run it using eclipse ... am new to java ... can u tell me wht Run configuration to use

thanks in advance

OmniXBro 06-03-2004 09:00 AM

isolated the problem some more. i couldn't import JFrame in javax.swing
but i could import anything in java.awt, and i'm guessing most of the other libraries as well.
in javax/swing/
there is a JFrame.h file, but that's it. um.......anybody help? ^^;;;;;;;
i tried removing import javax.swing.JFrame AND "extends JFrame" and it compiled.
thanks :)

DiWi 06-03-2004 09:11 AM

Hi
I was able to compile it or Eclipse does it automatically, when saving the file. It doesn't contain a main method ;)

In order to run it under Eclipse it's just go to Run/Run As/Java Application.
I recommend take a look on the Eclipse SDK. Especially for Java beginners, Eclipse has nice editing features like auto completion, rename, auto import selector and lot more.

Just go to http://www.eclipse.org and download the latest stable release and unix examples for Linux. If you're using Gnome or KDE select the GTK version. Installation is even easier: change to root and cd to /opt. Do a unzip <dist.package> and unzip eclipse-example.... as user just change to /opt/eclipse and start ./eclipse Take time and make familiar with the GUI (there's a tutorial within the product).

Dirk

Looking_Lost 06-03-2004 11:23 AM

Purely out of curiosity but what java sdk hava you got installed and where did you get it from?

aaa 06-03-2004 01:41 PM

Your code is mostly fine (it compiles properly as is), the problem is with your Java installation. Make sure you have the Sun Java SDK 1.2 or greater.

Here's your code, modified to run properly (it compiled before, just didn't run properly):
Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //for timed task's ActionListener
import java.io.*;

public class StopGo extends JFrame
{
  public static final int DELAY = 5000; //timer delay in milliseconds
  public static void main(String[] args)
  {
      //create new instance of StopGo
      final StopGo demo = new StopGo();
      //300x300 window
      demo.setSize(300, 300);
      //close when 'x' is pressed
      demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //show it
      demo.setVisible(true);

      //create task to perform
      ActionListener repaintJob = new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            demo.repaint(); //JFrame's repaint()
        }
      };

      //timer will perform it over specified interval
      Timer timer = new Timer(DELAY, repaintJob);
      timer.start(); //repeat repaintJob every DELAY milliseconds
  }

  //---- original code
  private final int d = 100, o=50;
  public void paint (Graphics g)
  {
      boolean condition = (Math.random() >= 0.5);
      if (condition)
        g.setColor(Color.red);
      else
        g.setColor(Color.green);
      g.fillOval(d,d,d,d);
      g.drawString ("Hello world!", 150, 150);
      g.setColor(Color.blue);
      g.drawOval(50, 100, 50, 50);
  }
}

This adds a main method so it'll run. paint() is executed only once (other than during window resizing), so a timer executes repaint() every 5 seconds, causing the paint() method to be called repeatedly.

OmniXBro 06-04-2004 01:08 AM

ummm thanks for the code :) i learn some stuff too but the problem is not the code. i think i have java 1.4 or something like that, i'll check again when i'm from that computer. but as i said, the problem is specifically in JFrame, since import javax.swing.*; didn't return any compilation error.
neither is any of the classes in awt. read my previous posts for more info :P
do i need to set a classpath? thanks...

DiWi 06-04-2004 02:53 AM

The exact version is displayed via java -version
In most cases you do not need to set the classpath, except you're using libraries, that are not under $JAVA_HOME/ and it's subdirectories.

Dirk


All times are GMT -5. The time now is 12:17 PM.