BigMoosie Posted December 21, 2005 Posted December 21, 2005 I am trying to learn Java but I have one problem that I cannot resolve, I don't know how to compile it. I tried downloading Sun's Java Developer Kit but it didn't put anything in my start menu after installation. I have a program called JBuilderX enterprise but it needs registration. How can I just get with the bloody coding! Sorry, not trying to be rude I'm just very frustrated because I cannot continue learning Java if I cannot actually compile it. Thankyou.
Aeternus Posted December 21, 2005 Posted December 21, 2005 Once you have the developer kit and it's installed, you need to find the directory where javac.exe is stored (probably something like C:\Program Files\Java\jdk... although it can differ quite alot so look around). Then open up a cmd prompt, go to the file you want to compile, then you need to set the path using set PATH=%PATH%;"$path_to_javac_directory" where $path_to_javac_directory is the path to the directory you found javac in. Then you can simply type "javac someFile.java" to compile someFile.java and then you can use "java someFile" to execute it (as the file should be named the same as the public class it contains). If you have any problems involving class defs not being found or other similar problems, you might have to add the current directory to the classpath (where java searches for class files when running and compiling other class files). This can be done in a similar way to setting the path - set CLASSPATH=%CLASSPATH%;"$path_to_current_directory" where $path_to_current_directory is the current directory ( for instance "c:\my project\"). This is all assuming you are using a windows system, if not say and I'll be happy to provide you with instructions for a linux system or help in finding those for for instance Mac OS X (similar to linux/unix nowdays id imagine due to the BSD-based backend). If you are looking for an IDE that will let you compile within it for ease of use, there are numerous free IDEs around, for instance there is BlueJ which is used in alot of Java courses in universities, there's Eclipse which is more powerful and features a whole host of addons and there are things such as NetBeans which is also very popular. All of these are free as far as I know.
clarisse Posted December 21, 2005 Posted December 21, 2005 You can also just Download JCreator (its free) Just google it.
BigMoosie Posted December 22, 2005 Author Posted December 22, 2005 Aueternus! Thankyou for that! I didn't realise it was quite so complex, I am working through compiling them myself and I'll check out those links in a couple of days. Once again thankyou very much.
BigMoosie Posted December 22, 2005 Author Posted December 22, 2005 Sorry to bother you again, after this I should be done and on my way, I have compiled a file into a .class but when I try to run it I get: c:\java>java hello Exception in thread "main" java.lang.NoClassGefFoundError: hello Here is my code: class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello"); } } What have I done wrong? EDIT: never mind, I read to type: set CLASSPATH=, I feel like an idiot, ill shut up now. Thanks for listening~
Aeternus Posted December 22, 2005 Posted December 22, 2005 Ah, I forgot to mention you can set the path more perminantly in Control Panel -> System -> Advanced -> Environment Variables -> Path and just adding the path to the end. Otherwise you end up having to use the set path command constantly and it gets rather annoying. ---------- EDIT--------- Just noticed your next post, heh. You should have the filename the same as the public class you are trying to compile, so you have HelloWorldApp as the classname, this should be in HelloWorldApp.java and when compiled will make a HelloWorldApp.class file, which you can run with "java HelloWorldApp". If it compiled, it would still have produced "HelloWorldApp.class" so you'll need to do "java HelloWorldApp". If you haven't compiled it with javac then you'll need to use javac first ("javac HelloWorldApp.java" then "java HelloWorldApp" to run).
BigMoosie Posted January 4, 2006 Author Posted January 4, 2006 Hi, sorry to bother you again but I am having another problem, it seems the book I am learning from is outdated by the compiler. When adding this to my code I get an error: public boolean action(Event evt, Object arg) { repaint(); return true; } Here is the error: Note: Applet1.java uses or overides a depreciated API Note: Recompile with -Xlint:depreciation for details Thanks again for helping me before, I swear I will try to make this the last time.
RyanJ Posted January 4, 2006 Posted January 4, 2006 I'm not shure but the name could be reserved, try changing the name and see if that fixes it Also, this may help you some: http://home.cogeco.ca/~ve3ll/jatutor0.htm Cheers, Ryan Jones
BigMoosie Posted January 4, 2006 Author Posted January 4, 2006 No it has nothing to do with filenames, could you please write a simple applet that when compiled demonstrates event handling? I swear I am about to tear my eyeballs out of their sockets, its not often that I find something so simple with coding that I cannot handle but it seems no matter what tutorials I read they explain a different way of doing it and none of them complile. Argh someone kill me, I am so damn frustrated!!!
Aeternus Posted January 5, 2006 Posted January 5, 2006 The error you list simply means that whatever method you are using or overriding is probably deprecated (ie it is no longer used or at least not the way you are using it). I think it will have compiled anyway (ie i think that is just a warning not an error) but obviously you'll want to read the API documentation for whatever Interface you are using to see why it might be giving you that warning (or as the warning says, use the option provided to get more details). I don't do alot of coding with Applets myself.... mainly because java applets in general get on my nerves and I try to avoid them when possible but as far as I know the idea behind the event handling is the same as in GUI programing. You have a component (ie your JApplet or JFrame or whatever you are trying to get an action for (JButton... whatever), you then somehow(*) implement an EventListener (will be called ActionListener, KeyPressListener, MouseListener or some other wierd name depending on what you are trying to do) in some class and the have an object instance of this class somewhere. Then you pass this object instance through to the method call addEventListener (or addActionListener etc etc) of the JFrame or... or JButton object you have. ---------------------------------------------- * - Ie this can be in one of your other classes, an anonymous member class as mentioned below or anything as long as you can make an instance object of it to pass into the addEventListener() type method. (ie this is javas way of handling function pointers and callbacks etc as obviously it doesnt have actual pointers (all objects are passed by reference) and certainly doesnt allow function/method pointers so interfaces and objects are the way it goes). ---------------------------------------------- This is then stored and when one of those events occurs the JButton or whatever will call the appropriate method that you've written to satisfy the interface implementation ( for instance with ActionListeners you have to implement a actionPerformed() method in whatever classes implements it and actionPerformed() is called ), and inside you can use the actual Event that is passed to these methods to determine more specifically what has happened (ie you know a key has been pressed but you want something to happen only when 'x' or 'y' has been pressed then you use the various Event methods to determine whether or not to do anything). This can cause problems as often when the particular method is called execution will not continue on the particular components thread of execution until the event handling method returns. This is problematic if you want to do long calculations based on an event as your whole app can seize up. What you can do in this situation is start another Thread to execute the operations you wish to occur and return immediately leaving that other Thread to do the work. This can prove troublesome sometimes as you might want access to certain things inside the class/object you implemented the EventListener Interface on but this can be fixed by simply using anonymous member classes which are just classes that you declare within a class or method that you can create an object instance with, that have the same access as the encompassing class or method (ie they can access the classes private fields etc). An example of what i mean is below - /** * Handles the MouseClick event for this Picture */ public void mouseClicked ( MouseEvent e ) { Runnable runSunset = new Runnable () { public void run () { sunset(); return; } }; Thread subThread = new Thread(runSunset); subThread.start(); } This was something I did when altering a simple assignment to start based on a MouseClick rather than straight away (wasn't necessary but was interesting to try). I could easily be wrong in alot of this, feel free to ask questions and if I am wrong, I am sure someone will correct me. There are probably easily and much tidier ways to do some of these things but these are just what I know etc. I'll try to knock together a simple applet that demonstrates this in a little while. [Edit] Knocked together a quick working example using the swing JApplet stuff, a button and mouse clicking. It allows you to click a button and it will change it's text (nothing special but it just illustrates how you can do it). import javax.swing.JApplet; import javax.swing.JButton; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; /** * Simple explanatory JApplet * * @author Matthew Gwynne * @version 05/01/2006 */ public class AppTest extends JApplet implements MouseListener { // Fields private JButton button; // Methods - Public /** * Constructor */ public AppTest() { // Init button = new JButton( "Hello" ); // Add Button this.getContentPane().add( button ); // Set up Event Listener button.addMouseListener ( this ); // Passing in this object // Show setVisible( true ); // This replaces show in Java 1.1 so.... could be the error } /** * Main method equivalent */ public void init() { AppTest x = new AppTest(); } // Methods - MouseListener /** * Handles Mouse Click Events */ public void mouseClicked(MouseEvent e) { if ( e.getButton() == e.BUTTON1 ) { button.setText("OMG NOOOO!!"); this.getContentPane().setVisible( false ); this.getContentPane().setVisible( true ); // Repaint the JApplet. } }; public void mouseEntered(MouseEvent e) {}; public void mouseExited(MouseEvent e) {}; public void mousePressed(MouseEvent e) {}; public void mouseReleased(MouseEvent e) {}; } [/Edit] Some Links - http://java.sun.com/docs/books/tutorial/uiswing/events/index.html http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JApplet.html http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/EventListener.html
BigMoosie Posted January 7, 2006 Author Posted January 7, 2006 Aeternus, gee I wish I could have a pocket version of you to sit on my desk. It is very kind of you to take the time to help me with this and not just shake me off with a couple of links. What added to all this confusion I was having was that some of my books were teaching the old way of doing events (apparently it was changed a few years ago) and others were teaching the new and I was picking up elements of both which caused all sorts of hassles. I cannot thank you enough, your help is very well appreciated. I don't know how I can repay you... if you ever need any help with JavaScript you can come to me, English is basically my second language after that.
Aeternus Posted January 7, 2006 Posted January 7, 2006 Glad it all worked out Thanks for the offer, I do do a few little things with JavaScript every now and again so I could very well be asking you a few questions at some point
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now