java

Discussion in 'Mac Programming' started by chris200x9, Jun 4, 2007.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    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.
     
  2. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2
    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.
     
  3. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #3

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

    plinden

    Joined:
    Apr 8, 2004
    #4
    Ugh ... I can't imagine trying to use either Notepad or TextEdit for programming.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    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
     
  7. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #7
    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!
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    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
     
  9. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #9
    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?
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    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
     
  11. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #11
    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?
     
  12. hawaiian macrumors member

    hawaiian

    #12
    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.

    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.
     
  13. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #13
    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");
    }
    }
     
  14. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #14
    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.
     
  15. hawaiian macrumors member

    hawaiian

    #15
    Yes, you'll need to set the class path. But I would think that most of the programs that you run while you're learning Java would start at the terminal. So setting the environmental variable is a personal choice you'll have to make.
     
  16. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #16
    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?
     
  17. hawaiian macrumors member

    hawaiian

    #17
    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...
     
  18. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #18
    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.
     
  19. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #19
    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
     
  20. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #20
    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.
     
  21. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #21
    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
    
     
  22. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #22
    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$
     
  23. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #23
    The easiest fix is to change directories first.
    Code:
    cd /Users/chris200x9/Documents/
    Then
    Code:
    javac myfirstapp.java
    then
    Code:
    java myfirstapp
     
  24. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #24
    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.
     
  25. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #25
    Cardinal rule #2
     

Share This Page