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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-03-2004, 12:35 AM
|
#1
|
|
Member
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48
Rep:
|
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 
|
|
|
|
06-03-2004, 03:39 AM
|
#2
|
|
LQ Newbie
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20
Rep:
|
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......
|
|
|
|
06-03-2004, 05:51 AM
|
#3
|
|
Member
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145
Rep:
|
I think by default that classes like awt and swing are included in the classpath.
Could you post your code?
|
|
|
|
06-03-2004, 07:23 AM
|
#4
|
|
Member
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48
Original Poster
Rep:
|
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!
|
|
|
|
06-03-2004, 07:52 AM
|
#5
|
|
Member
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64
Rep:
|
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.
|
|
|
|
06-03-2004, 08:52 AM
|
#6
|
|
Member
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65
Rep:
|
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
|
|
|
|
06-03-2004, 09:00 AM
|
#7
|
|
Member
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48
Original Poster
Rep:
|
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 
|
|
|
|
06-03-2004, 09:11 AM
|
#8
|
|
Member
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64
Rep:
|
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
|
|
|
|
06-03-2004, 11:23 AM
|
#9
|
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
Purely out of curiosity but what java sdk hava you got installed and where did you get it from?
|
|
|
|
06-03-2004, 01:41 PM
|
#10
|
|
Guru
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194
Rep:
|
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.
|
|
|
|
06-04-2004, 01:08 AM
|
#11
|
|
Member
Registered: Mar 2004
Location: Melbourne
Distribution: Mandrake 9.2
Posts: 48
Original Poster
Rep:
|
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...
|
|
|
|
06-04-2004, 02:53 AM
|
#12
|
|
Member
Registered: Jun 2004
Location: Frankfurt/M Germany
Distribution: SuSE 9.3/10.2/10.3
Posts: 64
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:37 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|