Eclipse wont run my class [message #1058844] |
Wed, 15 May 2013 07:55  |
Eclipse User |
|
|
|
OK, so I'm working through a book teaching me Java. They use NetBeans to test the code but I prefer Eclipse. So far so good but I can't get the following code to run in Eclipse but can in NetBeans using Run File rather than the normal Run Project.
class Printer {
int x = 0;
int y = 1;
void printMe() {
System.out.println("x is " + x + ", y is " + y);
System.out.println("I am an instance of the class "
+ this.getClass().getName());
}
}
class SubPrinter extends Printer {
int z = 3;
public static void main(String[] args) {
SubPrinter obj = new SubPrinter();
obj.printMe();
}
}
When I check the Run / Debug Properties (in Eclipse) it is empty whereas all my previous tutorials show the appropriate class in the Run / Debug settings.
Could someone explain what is happening.
My thanks for your interest.
|
|
|
|
|
Re: Eclipse wont run my class [message #1059471 is a reply to message #1059463] |
Sun, 19 May 2013 06:23   |
Eclipse User |
|
|
|
Roger,
Generally (at least when Auto Build is true), Eclipse compiles things as
soon as you save them, so generally the .class files are always up to
date with respect to the files in your workspace. More comments below.
On 19/05/2013 11:01 AM, Roger Wolfendale wrote:
> Ed, apologies for the delayed reply but many thanks, it worked fine.
> Just to clarify for any other newbies...
>
> 1. I right clicked the package that the 'Printer' class was contained
> in, 2. Selected Run As / Java Application,
> 3. Selected the 'Printer' class,
You could click directly on the Printer.java file...
> 4. From there the normal Run command worked as normal.
>
> I think item 3 re-compiled the class which still leaves me wondering
> what's going on as, quite separately, I had previously not only tested
> the class in NetBeans but also from the command line using javac
> Printer.java which created both the Printer.class and
> SubPrinter.class. From there invoking java SubPrinter ran the class
> successfully.
If there are multiple possible mains or the main isn't in the outermost
class, there's room for confusion; I think Eclipse will generally prompt
for such cases.
> Sorry to be a pest, Ed's suggestion worked fine and that should be
> enough but I don't exactly understand what Ed's suggestion achieved.
> Am I right in thinking that it basically compiled Printer.java and
> SubPrinter.java?
As I said above, that generally happens on save anyway. If you try to
run when there are unsaved changes, Eclipse generally asks if you want
to save such files and then it will compile them before trying to run
anything.
>
> Thanks again for your interest.
|
|
|
|
Powered by
FUDForum. Page generated in 0.11841 seconds