PDA

View Full Version : Java and XCode




Wowzera
Mar 16, 2009, 09:04 PM
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:

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

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!



dbell
Mar 17, 2009, 01:04 AM
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:

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

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!

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.

Wowzera
Mar 17, 2009, 10:10 AM
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.

Hum, ok, this is what I did:


package converser;
import java.util.*;

public class Test {

public static int simple (int x) {
return x + x;
}
}



First Class (Working methods)

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 ?

iShater
Mar 17, 2009, 10:26 AM
Put a debugging println in the Test class to make sure you are even getting there.

Wowzera
Mar 17, 2009, 10:51 AM
Put a debugging println in the Test class to make sure you are even getting there.

I keep getting only messages from the first Class :(

package converser;
import java.util.*;

public class Test {

public static int simple (int x) {
System.out.println("Hello!");
return x + x;

}

public static void main(String[] args)
{
System.out.println(simple(12));
System.out.println("Hi?");
}
}

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));

}
}

lazydog
Mar 17, 2009, 11:34 AM
Hi

Try swapping the order of your calls, ie


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

Wowzera
Mar 17, 2009, 11:46 AM
Hi

Try swapping the order of your calls, ie


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

Thanks,

After the clean I get this error message:

[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! :(

iShater
Mar 18, 2009, 07:50 AM
All file names match class names, etc?

I haven't touched XCode in a while, I Eclipse therefore I am. ;)

Wowzera
Mar 18, 2009, 10:48 AM
All file names match class names, etc?

I haven't touched XCode in a while, I Eclipse therefore I am. ;)

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

wrldwzrd89
Mar 18, 2009, 12:09 PM
Thanks,

After the clean I get this error message:

[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! :(
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.

Wowzera
Mar 18, 2009, 12:55 PM
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.
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 :(

wrldwzrd89
Mar 19, 2009, 12:20 PM
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 :(
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.

lazydog
Mar 19, 2009, 12:39 PM
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

wrldwzrd89
Mar 19, 2009, 12:42 PM
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
Did not know that... probably because I've never tried such a thing. Thanks. :)

Wowzera
Mar 19, 2009, 09:23 PM
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

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?

lazydog
Mar 20, 2009, 04:42 AM
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

milk242
Mar 20, 2009, 04:57 AM
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?

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

Wowzera
Mar 20, 2009, 10:31 AM
@ 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

lazydog
Mar 20, 2009, 11:42 AM
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

Wowzera
Mar 20, 2009, 03:38 PM
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

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