LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-03-2004, 12:35 AM   #1
OmniXBro
Member
 
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48

Rep: Reputation: 15
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
 
Old 06-03-2004, 03:39 AM   #2
keikun_naruchan
LQ Newbie
 
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20

Rep: Reputation: 0
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......
 
Old 06-03-2004, 05:51 AM   #3
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

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

Could you post your code?
 
Old 06-03-2004, 07:23 AM   #4
OmniXBro
Member
 
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48

Original Poster
Rep: Reputation: 15
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!
 
Old 06-03-2004, 07:52 AM   #5
DiWi
Member
 
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64

Rep: Reputation: 16
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

Last edited by DiWi; 06-03-2004 at 07:54 AM.
 
Old 06-03-2004, 08:52 AM   #6
linux_ub
Member
 
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65

Rep: Reputation: 17
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
 
Old 06-03-2004, 09:00 AM   #7
OmniXBro
Member
 
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48

Original Poster
Rep: Reputation: 15
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
 
Old 06-03-2004, 09:11 AM   #8
DiWi
Member
 
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64

Rep: Reputation: 16
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
 
Old 06-03-2004, 11:23 AM   #9
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Purely out of curiosity but what java sdk hava you got installed and where did you get it from?
 
Old 06-03-2004, 01:41 PM   #10
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
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.
 
Old 06-04-2004, 01:08 AM   #11
OmniXBro
Member
 
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48

Original Poster
Rep: Reputation: 15
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...
 
Old 06-04-2004, 02:53 AM   #12
DiWi
Member
 
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64

Rep: Reputation: 16
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java JFrame sizes. byteframe Programming 8 10-25-2005 02:09 AM
java tree class true_atlantis Programming 1 08-30-2005 12:03 PM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 04:57 AM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM
(jwstric2) JFrame error on JAVA nedian123 Programming 2 07-02-2004 05:10 AM


All times are GMT -5. The time now is 08:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration