Java Noob Help

Discussion in 'Mac Programming' started by Jasonbot, Sep 16, 2006.

  1. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #1
    I need some very basic help with Java and XCode.
    I've just started java at school, using PC's with JCreator, and now when I take my project home and run it in Xcode I can't compile or anything.
    In fact most of the options are grayed out.

    Can anyone help me?

    Here's the code:

    Code:
    import javax.swing.*;
    
    public class variables 
    {
    	public variables ()
    	{
    	
    	String  name = "Jason";
    	String  surname = "BoTHa";
    	int 	age = 17;
    	char gender = 'm';
    	double height = 148.9;
    	
    	boolean married = true;
    	short treasures_found = 12;
    	byte shot = 4;
    	float bank_account = 8.1f;
    	long distance_traveled = 7834;
    	
    	JOptionPane.showConfirmDialog(null,name);
    
    	
    	}
        
        public static void main(String args[]) 
        {
           new variables ();
        }
    }
    
    
    THANX!
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You cannot just open a java file in XCode and compile/run it. The only way to use XCode is with XCode projects. You need to start a new XCode Java project and add the file to it.

    Or since you are trying to learn something find out what's going on in all these IDEs. Use the command line javac to compile the file(s) and java to run then.
     
  3. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #3
    Whoa :confused: OK, I tried the whole Xcode project thing -Really confusing! What should I use for java?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Probably Java Tool...
     
  5. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #5
    Is this a seperate IDE? Link...
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    No it's a choice in XCode. When you choose New Project scroll down to the Java section then choose Java Tool (it's the last item in there). If I were you I'd learn to use the command line tools. It'll be useful in the long run anyway.
     
  7. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #7
    Ok now there's a prblem under projects there's no java section???
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Which version of XCode are you using? Did you install ALL the optional support packages as Java project templates may not be installed by default...

    I see the Java section on XCode 2.3 on OSX 10.4.7
     
  9. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #9
    You should see something like the screen shot I've attached.
     

    Attached Files:

  10. macrumors 6502

    Joined:
    Sep 15, 2006
    #10
    well it's been mentioned earlier, try finding your project using the terminal.
    When found use
    Code:
    javac *filename*
    which will try to compile your application, if it works then you know that you have a valid distribution of java installed (which afaik should be default, but still worthwhile to check).

    The project you've posted seems kinda strange to me overall, but when you begin programming I suppose everyone has to find their own way to implement stuff - based on personal prefences and structure.

    You could also try to install eclipse instead (http://www.eclipse.org/), which is a nice alternative to xcode an is available on all platforms - free
     
  11. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #11
    I'll try re-install Xcode when I get back to my mac tomorrow. Until then is java and standard option when installing xcode? or is it one of those custom check box options??
     
  12. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    Can't remember TBH, look through the options and install all java related ones, you should also get the latest version of the Java development software from Apple's Developer website.

    You will probably find it easier to compile files via the command line using the command javac but a Java Tool Project should be fine.

    I would say that this isn't as easy as it should be.
     
  13. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #13
    There are custom options? :D I've never done anything but a standard installation and gotten Java, along with everything else.
     
  14. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #14
    Is xcode really that hard for java compiling? I think I'll try Eclipse. Right Now I'm using J J Edit It's only a few kbs and it's freeware
     
  15. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #15
    It's not that Xcode makes Java compiling difficult, but like many IDEs, it works from a project. It shouldn't force you into the project but it's a choice their designers made.

    Eclipse really doesn't seem any easier to use--it's just different. JEdit seems to only be an editor--you can't create a product with it. You'll find quite a few threads on everyone's favourite tools here. I use a number of them but for Java, I tend to stick with jGrasp and NetBeans for smaller projects.
     
  16. macrumors 68000

    Joined:
    Sep 8, 2006
    #16
    Hey I just started learning Java and you are way ahead of me. Just for fun on my side I copied your code and pasted it into jEdit which is the program that I am learning with and then I use the Terminal to complie it. It did pop up with a result.

    I understood some of your code since that is the chapter that I am reading right now. But what popped up after compiling the code and running it wasn't what I thought was going to pop up.

    But it seemed to work fine using that method. I hope that ws OK that I ran your code as a fun test for my self.

    -Lars
     
  17. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #17


    Wow, I have a fan! I really don't mind that you used My code. It was my 3rd lesson @ school, I hope I'll become a really 1337 programer one day.

    Are you learning from a book or something? Is it hard?
     
  18. macrumors 6502a

    Joined:
    Nov 24, 2005
    #18
    Use Eclipse for pure Java, forget XCode. XCode is good for many things, Pure java isn't one of them. It works, but there are better options out there.
     
  19. macrumors 68000

    Joined:
    Sep 8, 2006
    #19
    yap, Java for Dummies book. I'm not planning on being a programmer but I need some software for a hotel project to communicat with many hotels using Telnet and Java. There is no spacific program for what I need so I decuded to learn Java to try to create it.

    The one thing with your code what I was expecting was to see the other stuff like Age, and numbers poping up since you used the INT / DOUBLE /SHORT /FLOAT and so on but all that I got was a window that popped up with your name on it and the Cancel / No /Yes button.

    What did all that other stuff do since it was typled in? I twas neat seeign the code since I just read that page the other night on CHAR / FLOAT / SHORT /BYTE and so on. and having to state what your Variables are before you declare a Variable, if I got that right.

    -Lars
     
  20. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #20
    No, my code's very basic. It's just to outline how you can use variables instead of typing in the different numbers/characters everytime. I don't know much yet, mybe next lesson we'll learn how to make the variables do some cool stuff?

    But yes, you have to declare your variables in the variables part of teh code before you can use them...
     
  21. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #21
    Cool, xcode works! I used the right project and everything goes smoothly! But is there anyway to make a .class file instead of a .jar file? Thanx:D
     
  22. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #22
    I don't know of a way but the .class file is in the .jar (Java ARchive) file. Besides that, the .jar file can be double-clicked on Mac OS X to launch it.
     
  23. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #23
    I guess, I could also compiule it in terminal.

    Just some basic text colour help now -How do I know what colour text means what?

    EDIT: I understand about the colours now. I want to know how I can make the syntax colours in xcode look similar to the ones in JCreator(the app we are using @ school)
     
  24. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #24
    You can find/change all of the colours in Xcode preferences (font and colour options), basically green is a comment, red is a string of text, blue is a character/number, purple is a keyword (eg for java, int, float, public, static, import etc. etc.), black is any other text in the document.
     
  25. thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #25
    Ok, The colours are just confusing me because I used (Useless) JCreator @ school :(
     

Share This Page