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

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
hi I noticed text edit won't save as .java. What do I download/install to use java in text edit? or how? sorry if this is a dumb question I know I could just use XCODE but I'm just learning java and the book says not to use an IDE as it can hamper learning.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
TextEdit is just a simple word processing app and isn't suitable for programming. Try TextWrangler

I'm sure there'll be other recommendations. Personally, I use gvim.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
TextEdit is just a simple word processing app and isn't suitable for programming. Try TextWrangler

I'm sure there'll be other recommendations. Personally, I use gvim.


that book lied to me! lol they said use a word processor like note pad or if on a mac text edit :eek:
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Ugh ... I can't imagine trying to use either Notepad or TextEdit for programming.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
As bad as Text Edit will be as a programmers editor it certainly will save as .java as long as you understand how to use it:

1) Make sure you are in plain text mode. You cannot save styled text as .java (obviously). Go to the Format menu. If there is a menu item called "Make Plain Text" you are in rich text mode. Use that menu item to turn it to plain text

2) When you are saving your plain text file make sure the "if no extension if provided, use ".txt"" option is unchecked and save your file as whatever you want.java.

Simple really
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
TextEdit will work fine. You just have to set your preferences to save as a plain text file. The default format is rtf.

TextEdit>Preferences>and radio button Plain Text

Todd
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
so ok I CAN do it in text edit but I Could do it easier in something like textwrangler? is that what you're saying?

and also I know you said it would be a pain in text edit but my question is do you think I would LEARN more with text edit or textwrangler?


by the way thank you guys alot for your quick and helpful replies!
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
I use TextWrangler and it is a good, basic, syntax-highlighting editor. Works well with Java. I've had fewer issues sending TextWrangler'ed files to Windows than I have with TextEdit also.

Todd
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
ok thank you!

One more question and then to compile it I just open up terminal and type %javac and then the path to the file?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Pretty much. I usually navigate to the directory in Terminal first, and then just enter

javac MyProg.java

Then to run,

java MyProg

Easy as pie. Todd
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
thank you so much!


edit: ok so I compile it in the terminal it returns myapp.java as myapp.class which i think is right...then how do i run my class clicking it doesnt do anything?
 
Ugh ... I can't imagine trying to use either Notepad or TextEdit for programming.

Hey! I used notepad all the time when I was in high school! But...that having been said, it sucked =/. No color, no auto tabbing.

thank you so much!


edit: ok so I compile it in the terminal it returns myapp.java as myapp.class which i think is right...then how do i run my class clicking it doesnt do anything?

Ok, so you can't just double click on the .class file. From the terminal you probably said something like:

> javac myapp.java

In order to run your app, you'll need to do:

> java myapp

That will start your program at the main method defined in your myapp class.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
ok first of all I don't know weather to use just java MyFirApp or java the path and then the either .java or .class extension?

secondly no matter what I do I get the message Exception in thread "main" Java.lang.NoclassDefFounderror:MyFirstApp


and incase my code is wrong it is


public class MyFirstApp {
public static void main (String [] args) {
System.out.println("I rule");
System.out.println(The world");
}
}
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
You don't have the classpath set.

Type:
Code:
java -cp . MyFirstApp

-cp . tells the java process to look in the current directory for the .class file named MyFirstApp.

Some people set the CLASSPATH environment variable like this, meaning they don't have to type -cp .:
Code:
export CLASSPATH=.

I don't like doing this since I think it's a bad habit to get into. It's less flexible than specifying the class yourself, and generally you won't launch from the command line for real usable programs.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
Type:
Code:
java -cp . MyFirstApp

-cp . tells the java process to look in the current directory for the .class file named MyFirstApp.

ok do I type that at the begining of my code or in the terminal when I try to run it?
 
Type:
Code:
java -cp . MyFirstApp
-cp . tells the java process to look in the current directory for the .class file named MyFirstApp.

ok do I type that at the begining of my code or in the terminal when I try to run it?

That would be a terminal command. At the terminal, the first string tells the system what program to run. The following strings are parameters given to the program. You should be able to do something similar with your code. For example, if your code is:

Code:
public class Test {
    public static void main(String[] args) {
        if(args.length >= 2)
            System.out.println(args[0] + ", " + args[1]);
    }
}

Then you can do the following behavior at the terminal:

> javac Test.java
> java Test Hi mom
Hi, mom

So what this shows is that programs like javac and java are run (actually "forked" or spawned off as another process) by the terminal and the following strings are passed to the program in turn. So the string set {"Test", "Hi", "mom"} is passed to the program java. java then runs the Test program and passes the string set {"Hi", "mom"} as parameters to that program. And then in our program, we simply printed them back out to the command prompt =/.

Hope this helps clear stuff up =). Even though that wasn't your original question...
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
You don't need to set the classpath explicitly, current directory is always implied.

I am guessing you broke cardinal rule #1.

The name of your class and the name of the source code file (minus the extension) should be the SAME.

In your code example above, your file name should be MyFirstApp.java, not myapp.java - and yes, it IS CASE SENSITIVE. Then follow the advice of the others on how to compile and run. This should be explained in your book.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
You don't need to set the classpath explicitly, current directory is always implied.

I am guessing you broke cardinal rule #1.

The name of your class and the name of the source code file (minus the extension) should be the SAME.

In your code example above, your file name should be MyFirstApp.java, not myapp.java - and yes, it IS CASE SENSITIVE. Then follow the advice of the others on how to compile and run. This should be explained in your book.

yea its not my book is kinda crappy in that respect it is more of the why and how of actual code, rather than how to run it
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
ok I tried defining the class path but I still get the Java.lang.NoclassDefFounderror:MyFirstApp error? what am I doing wrong? should I have downloaded something? and yes my source file and my class file have the EXACT same name except the extension.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
ok I tried defining the class path but I still get the Java.lang.NoclassDefFounderror:MyFirstApp error? what am I doing wrong? should I have downloaded something? and yes my source file and my class file have the EXACT same name except the extension.

attach your source, and the exact terminal window contents (where you are trying to compile and run).

Edit:
Here is how it is done and although this was done in Cygwin, the commands are the same. Also, the cat command is just to show the source I am compiling.

Code:
ckingj1@CKINGJ1PC1 ~
$ cat MyFirstApp.java
public class MyFirstApp {
  public static void main (String [] args) {
    System.out.println("I rule");
    System.out.println("The world");
  }
}

ckingj1@CKINGJ1PC1 ~
$ javac MyFirstApp.java

ckingj1@CKINGJ1PC1 ~
$ java MyFirstApp
I rule
The world
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
heres my exact terminal output

Last login: Wed Jun 6 14:44:59 on ttyp1
Welcome to Darwin!
chris-chmielewskis-computer:~ chris200x9$ javac /Users/chris200x9/Documents/myfirstapp.java
chris-chmielewskis-computer:~ chris200x9$ java myfirstapp
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp
chris-chmielewskis-computer:~ chris200x9$
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
heres my exact terminal output

Last login: Wed Jun 6 14:44:59 on ttyp1
Welcome to Darwin!
chris-chmielewskis-computer:~ chris200x9$ javac /Users/chris200x9/Documents/myfirstapp.java
chris-chmielewskis-computer:~ chris200x9$ java myfirstapp
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp
chris-chmielewskis-computer:~ chris200x9$

The easiest fix is to change directories first.
Code:
cd /Users/chris200x9/Documents/

Then
Code:
javac myfirstapp.java

then
Code:
java myfirstapp
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If you do a pwd what directory are you in? You need to run the java command from the exact same directory that the .class file is in.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.