Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
You should see something like the screen shot I've attached.
 

Attachments

  • Picture 1.png
    Picture 1.png
    45.5 KB · Views: 100

mufflon

macrumors 6502
Sep 15, 2006
264
2
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
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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??
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Jasonbot said:
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??

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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Jasonbot said:
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??

There are custom options? :D I've never done anything but a standard installation and gotten Java, along with everything else.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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.
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
laswik said:
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



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?
 

odedia

macrumors 65816
Nov 24, 2005
1,047
157
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.
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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...
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Jasonbot said:
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

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.
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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)
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.