Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Eclipse wont run my class
Eclipse wont run my class [message #1058844] Wed, 15 May 2013 11:55 Go to next message
Roger Wolfendale is currently offline Roger WolfendaleFriend
Messages: 3
Registered: May 2013
Junior Member
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 #1059267 is a reply to message #1058844] Wed, 15 May 2013 12:57 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Roger,

Right click on the "main" method and invoke Run As -> Java Application.
Once you've done that once, a Run Configuration will be created and that
will appear in your Run menu.


On 15/05/2013 2:54 PM, Roger Wolfendale wrote:
> 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.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Eclipse wont run my class [message #1059463 is a reply to message #1059267] Sun, 19 May 2013 09:01 Go to previous messageGo to next message
Roger Wolfendale is currently offline Roger WolfendaleFriend
Messages: 3
Registered: May 2013
Junior Member
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,
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.

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?

Thanks again for your interest.
Re: Eclipse wont run my class [message #1059471 is a reply to message #1059463] Sun, 19 May 2013 10:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
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.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Eclipse wont run my class [message #1059518 is a reply to message #1059471] Mon, 20 May 2013 08:34 Go to previous message
Roger Wolfendale is currently offline Roger WolfendaleFriend
Messages: 3
Registered: May 2013
Junior Member
Well done Ed, thanks for your help and patience. Much appreciated.
Previous Topic:plugin for Sybase Power Builder
Next Topic:editor questions
Goto Forum:
  


Current Time: Tue Apr 23 09:16:23 GMT 2024

Powered by FUDForum. Page generated in 0.09730 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top