Java and XCode

Discussion in 'Mac Programming' started by Wowzera, Mar 16, 2009.

  1. macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #1
    Hello,

    When I try run Java code in Xcode it just pops up with a window that says in big blue letters "Java Application" and when I look in the console for stuff I've output via System.out.println() it's empty.

    So, if anyone out there uses Xcode for Java, could you enlighten me as to what I'm doing wrong?

    I can't even make Xcode display the result of a conversion Celsius to Kelvin.

    Here's my code:

    Code:
    public static double celsiusToKelvin (double celsius) {
           return celsius + 273;
    }
    Code:
    public static void main (String[] args) {
           System.out.println(celsiusToKelvin(10.3));
    }
    

    EDIT:


    I got it now...
    Theres one more question:

    How can I run two .java files at once?
    Like, I created both on the same project with each one with its own main method, but everytime I open the console and run the files, I just see the results of the first one!
    Example:
    I did the method above, then I created another java class to make a method Celsius to Fahrenheit and want to print it on the console, but I just see the results of the Celsius to Kelvin!

    Can anyone help me?
    Thanks!
     
  2. macrumors member

    Joined:
    Jul 11, 2007
    #2
    You wouldn't run them from two main()'s you'd run them with the same one.

    e.g.

    public static void main(String[] args) {
    System.out.println(method1());
    System.out.println(method2());
    }

    They don't need to be in the same class file. You could have them main in one file, e.g. Run then you conversion calls in two other classes.
     
  3. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #3
    Hum, ok, this is what I did:

    PHP:
    package converser;
    import java.util.*;

    public class 
    Test {
        
        public static 
    int simple (int x) {
            return 
    x;
        }
    }


    First Class (Working methods)
    PHP:
    package converser;
    import java.util.*;

    public class 
    Converter {
        
        public static 
    double celsiusToKelvin (double celsius) {
            return 
    celsius 273;
        }

            public static 
    void main (String args[]) {
            
    System.out.println(celsiusToKelvin(20.2));
            
    System.out.println(Test.simple(2)); // Has no errors, but does not work, does not print on console.
            
    }


    What am I doing wrong ?
     
  4. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #4
    Put a debugging println in the Test class to make sure you are even getting there.
     
  5. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #5
    I keep getting only messages from the first Class :(

    PHP:
    package converser;
    import java.util.*;

    public class 
    Test {
        
        public static 
    int simple (int x) {
            
    System.out.println("Hello!");
            return 
    x;
            
        }
        
        public static 
    void main(String[] args)
        {
            
    System.out.println(simple(12));
            
    System.out.println("Hi?");
        }
    }
    PHP:
    package converser;
    import java.util.*;

    public class 
    Converter {
        
        public static 
    double celsiusToKelvin (double celsius) {
            return 
    celsius 273;
        }

        public static 
    void main (String args[]) {
            
    System.out.println(celsiusToKelvin(20.2));
            
    System.out.println(Test.simple(2));
            
        }
    }

     
  6. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    Hi

    Try swapping the order of your calls, ie

    Code:
    System.out.println(Test.simple(2));
    System.out.println(celsiusToKelvin(20.2)); 
    
    Also try a 'clean' before compiling.

    It could be for some reason your program isn't picking up the second class Test properly, either it is missing or you have an old compiled one.
    b e n
     
  7. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #7
    Thanks,

    After the clean I get this error message:

    Code:
    [Session started at 2009-03-17 13:42:57 -0300.]
    Exception in thread "main" java.lang.NoClassDefFoundError: Converter
    
    The Debugger has exited with status 1.The Debugger has exited with status 1.
    What could be wrong? The code does not look to have any kind of error.

    All my questions may sound that I am dumb, but its the first time I am touching on XCode! :(
     
  8. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #8
    All file names match class names, etc?

    I haven't touched XCode in a while, I Eclipse therefore I am. ;)
     
  9. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #9
    I deleted that project and created a new one.
    But, I do not know how to work with static methods or two main methods (plubic static void main~). If I create a object I can work with them.

    Also, another issue I have is that everytime I need to build and see the results I must save the classes, it does not save automatically, is there a way to do it ?

    Thanks iShater! :) :D
     
  10. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #10
    Is the Converter class in the folder Java's looking for it in? According to your code, it's defined as being in the converser package. Therefore, both classes should be placed in a converser folder in your project folder, so Java can find them.
     
  11. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #11
    It happened after I used the "Clean" command on Xcode. The reason of the error may be because Xcode cleaned some essencial files and I didn't/don't know how to create them back.

    Now, I just don't know how to create two main methods (public static void main~), as I said above :(
     
  12. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #12
    You can't make two main methods and have them both run in the same Java project. That won't work. What you can do is make two separate projects, each with its own main method, and run them independently.

    The clean command in Xcode deletes only build files, which can easily be regenerated with the build command. Also, I point to my earlier post about packages - are you sure the structure is correct? Java will complain if it's not.
     
  13. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #13
    Um... actually it's not a problem to have two or more main() methods in the same application/project so long as they are in different classes. In fact, some people use a main() in each class to perform testing of the class.

    b e n
     
  14. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #14
    Did not know that... probably because I've never tried such a thing. Thanks. :)
     
  15. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #15
    If I am using the same project I am not able to.
    All I have is results from the first main method :(
    How can I do it? I need to erase other main methods to do it?

    @ wrldwzrd89

    I did "Clean" then I got that error I posted earilier. Couldn't manage to get it back, tried everything, build, run, etc.. :(



    Btw...

    Is there a way to build and run java files without being on a project on Xcode?
     
  16. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #16
    Hi Wowzera, any chance you could zip your project up and post a link to download it? It would make it easier to work out what's wrong with it.

    b e n
     
  17. macrumors 6502a

    Joined:
    Jun 28, 2007
    #17
    I'm not too familiar with xcode but you can build and run java through terminal. Use "javac" to build and compile the java file and "java" to run the file.

    Plus, I don't see any error when running your code in xcode
     
  18. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #18
    @ lazydog


    I deleted the one I mentioned on the main post, I am with a new one. I just have two questions right now:

    - How can I use two main methods on the same project?
    - How can I build and run a .java file created on XCode without build a new project. Just the java class, then run and execute it on the XCode.

    Download the project here: http://kttns.org/1odf


    @milk242

    I had no problems with that code before I did the clean, couldn't understand why it didn't work until now! If I copied the code and pasted it on a new project, then I could build and run it.


    Thank you all guys for trying to help! I really appreciate that! :D
     
  19. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #19
    Hi Wowzera

    Okay, your project runs fine on my Mac so I'm a bit confused what it is exactly you want to do.

    If you put a static void main() in Exercicios then you can call it from Main.java easily enough. Or is it you want to be able to run Exercicios.main() instead of Main.main() when you hit the run button? If that's what you want to do then all you need to do is add a new executable to the project. In the details list look under Executables, you should see one called java. Copy it and then open up the info panel for it. Change the argumets to it from -cp Main.jar Main to -cp Main.jar Exercicios. You can then quickly swap between running Main and Execicios using the active executable drop down menu in the tool bar (you may need to add the menu if it's not already there).

    b e n
     
  20. thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #20
    Thank you !

    I want to work with two or more static void main() methods. I want code on a class and don't need to swap java files to test my methods, using the static void main().
    I couldn't copy the java file inside Executables, but, I can now use the static void main() in Exercicios, I just added -cp Main.jar Exercicios, but I can only work with one of these, if I select both, it will work only with the first one, but I can select which I can work from there, just selecting it. I guess it is how it should work, is it is, then all my questions are solved.

    Thank you very much lazydog!! :D
     

Share This Page