• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to run files in package through Eclipse  RSS feed

 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to java. Please help me.
there are 2 files

File1 - Beverage.java

package cert;
class Beverage { }

File2-Tea.java

package exam.stuff;
import cert.Beverage;
class Tea extends Beverage
{
public static void main(String[] a)
{
System.out.println("Hello");
}
}

1. I want to know how to run the file Tea.java in the console and get the error message
"Can't access class cert.Beverage. Class or interface must be
public, in same package, or an accessible member class.
2. I have used Eclipse to run the program, but did not get the above error message. Instead i received different error message. Please find

I am preparing for SCJP1.6. this example appeared in the very beginning, and its kind of work stopper for me.
So it would be great if you can help me with this ASAP.
Thanks in advance.
/Users/popo/Desktop/Screen Shot 2012-09-04 at 3.07.37 PM.png
ex.jpeg
[Thumbnail for ex.jpeg]
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare class Beverage as public, hence:

Otherwise, the class will be accessible to only those classes that are in the same package.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can’t read your screenshot. Please post the text.
As for running something from the command line, try our FAQ.
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Kemal Sokolovic. Thanks for the reply. Yes if I add public to Beverage the problem is resolved. But I wanted to see the error message " Can't access class cert.Beverage. Class or interface must be public, in same package, or an accessible member class. import cert.Beverage;" while i am running this program in eclipse. I do get this error message in command line when I try to run the same program (please find the attachment.)
Basically I am preparing for the SCJP exam. This is topic is covered in the first chapter in Kathy Sierra's book.

@Campbell Ritchie. Thanks for the reply.
Somehow I do not see the attachments in preview. Hence uploaded in picasa. Here the link.
https://plus.google.com/u/0/photos/115635397963444901611/albums/5784473501617456609/5784473524757071106
Please find the text below

Output from eclipse
=============
Exception in thread "main" java.lang.IllegalAccessError: class com.Tea cannot access its superclass example.Beverage
output from command line
=================
/Users/popo/poongothai/Tea.java:5: one.Beverage is not public in one; cannot be accessed from outside package
class Tea extends Beverage
^
1 error
Screen-Shot-2012-09-04-at-7.43.27-PM.png
[Thumbnail for Screen-Shot-2012-09-04-at-7.43.27-PM.png]
Screen-Shot-2012-09-04-at-3.07.37-PM.png
[Thumbnail for Screen-Shot-2012-09-04-at-3.07.37-PM.png]
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you won't always get the same error message on command line and IDE you're using. In some cases there might be even a difference between different IDEs. Try to run your first code in NetBeans and you'll probably get something else as an error message. The point is - whatever you get, it should point you to the right direction to correct your code.
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Kemal Sokolovic. Thank you very much. As I told you I am preparing for SCJP. I should be prepared to answer the question like "What is the compilation error for the following program?". In this case, I need to select the exact error type, isn't? Thats the reason I am worried. For the SCJP exam's perspective, should I try running the small sample programs in IDE or command line?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know exactly, but I think you should prefer command line when preparing for SCJP.
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:I don't know exactly, but I think you should prefer command line when preparing for SCJP.


Thanks for the help
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans uses the Java compiler in the JAVA_HOME location in the standard PATH on your PC. Eclipse has its own compiler, and it is bl**d* good. So Eclipse produces different error messages.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!