How to extend a class in same package
I have a class called LoginBean.java which extends the class ConnectDB.java
this both files have a statement both the classes are under the directory pmtools.
and both are included in package pmtools.
(they both have statement "package pmtools; " at top)
when i am trying to compile LoginBean.java, it is giving me following error,
LoginBean.java:7: cannot resolve symbol
symbol : class ConnectDB
location: class pmtools.LoginBean
public class LoginBean extends ConnectDB
^
LoginBean.java:59: cannot resolve symbol
symbol : variable dbConn
location: class pmtools.LoginBean
stmt = dbConn.createStatement();
^
2 errors
did you compile the other class already? donnow if ithahahaha!!!!!
ok include the import statement
import pmtools.ConnectDB;
in the second class and than compile..
Similar Messages
-
How to extend a class from super package?
Say I have a dir a\
in dir a\ there is a dir b\
in b\ there is a class called DerivedClass
in a\ there is a class called ExtendedClass
how do I extend ExtendedClass from DerivedClass?package a.b;
import a.ExtendedClass;
public class DerivedClass extends ExtendedClass{
//define class members
This is interesting. But the question is, how does java know where is package b?? By the classpath variable - the classpath is set to one directory above 'a'. When you say
a.ExtendedClass, since the classpath is set to the immediate parent of a, it searches the child directories
for a directory called 'a' and upon finding it, searches for a .class file 'ExtendedClass'.
Similarly when you say a.b.DerivedClass, the search begins in the parent directory for a directory called a and then
a directory 'b' inside it on finding which it searches for DerivedClass.class
Alternatively you could have defined DerivedClass to be in package 'b'.
package b; //note the difference. Its b and not a.b
import a.ExtendedClass;
public class DerivedClass extends ExtendedClass{
//define class members
}in which case, you should set the classpath to
1. a's parent directory
2. a itself
The parent directory cp is still required to locate a.ExtendedClass
The fully qualified name of DerivedClass is b.DerivedClass
Thus if #1 alone were present, since 'b' is not directly inside the parent directory,
b.DerivedClass would never be found
Hence the #2 is required-the search should start from the directory 'a' (as directory 'b' is inside 'a').
therefore, a package is a directory.No, package is a very java specific thing and though logically package names should match directory names, they
arent the same.
cheers,
ram. -
Anyone know how to find available classes in a package?
Anyone know how to find available classes in a package?
Given a String like "java.io" I would like to write a method to extract the available classes in this package. I can't seem to find a method for this anywhere in the docs. The package class does not seem to have a method like this.Here is some code I tried.
I'm not very familiar with manipulating JARs The code below is what I tried, but how do I access the packages like "java.io" in the JarFile? and then get the available classes?
import java.io.*;
import java.util.*;
import java.util.jar.*;
class Tester
public static void main(String args[])
try {
JarFile jf = new JarFile("c:/jdk1.3/lib/dt.jar");/* usually rt.jar (and 1.4)*/
Enumeration e = jf.entries();
while (e.hasMoreElements())
Object current = e.nextElement();
System.out.println(current.toString() + "class:"+current.getClass());
try {
Thread.sleep(300);
} catch (InterruptedException ie) {}
} catch (IOException ioe) {System.out.println(ioe.toString());}
/*also tried ClassLoader cl = ClassLoader.getSystemClassLoader();
try {
Enumeration e = cl.getResources("c:/jdk1.3/lib/dt.jar");
System.out.println(e.nextElement());
while (e.hasMoreElements())
System.out.println(e.nextElement());
} catch (IOException ioe) {System.out.println(ioe.toString());} -
Implemenet/Extend interface/class in default package
Filename: DeaultInterface.java
public interface DefaultInterface {
// Abstract Methods...
Filename: ConcreteClass.java
package com.company;
import DefaultInterface;
public class ConcreteClass implements DefaultInterface {
// Implementation of Abstract Methods...
}When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".
Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
Edited by: 974531 on Dec 2, 2012 11:07 PM>
When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".
Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
>
You CAN implement the interface defined in the default package.
What you CANNOT do is import it. And that is because the Java Language Specification specifies that named types can only be referenced by their simple name if they are imported and must otherwise be referenced by their fully qualified name.
See the 7.5 Import Declarations in the Java Language Specification - http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html
>
An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.
Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (§6.7).
A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.
>
So you can't reference that type by importing it because you can't import a type that doesn't have a canonical nmae. And you can't just use the simple name because, as the first statement above says you have to import it to use the simple name.
When you use the simple name of that type without an import the simple name would refer to a class IN your 'com.company' named package. -
How to import a class from a package at the same level
Hello friends,
i have a class as Plaf.java
as
package org.vaibhav.swing.plaf;
import java.awt.*;
import javax.swing.*;
public class Plaf {
other code
}as you see, this is in org.vaibhav.swing.plaf package.
I have one more class as
package org.vaibhav.swing.frames;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StartFrame extends JFrame implements ActionListener {
other code
}and this class is in package org.vaibhav.swing.frames
Please hlp me how to use the class Plaf in StartFrame.java. Rather what import statement should i use in StartFrame.java.
please help.
thank you
Message was edited by:
vaibhavpinglebut it then gives me this error
StartFrame.java:11: package org.vaibhav.swing.plaf
does not exist
import org.vaibhav.swing.plaf.*;
Have you first compiled Plaf.java and saved it in this directory strucuture
/org/vaibhav/swing/plaf/
And also have you set your classpath to the parent directory of org folder.
Message was edited by:
qUesT_foR_knOwLeDge -
Error creating instance of class from same package
When I try to create an instance of a class that is in the same package, my IDE indicates that the constructor can not be found. Can anyone tell me what is wrong? Thanks. Below are the codes for both classes:
package com.practice;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class WebProject extends Applet{
public void init(){
// The following line is where the IDE indicates there is an error
UserInterface gui = new UserInterface();
} // end init()
public void start(){
} // end start()
public void stop(){
} // end stop()
public void destroy(){
} // end destory()
} // end class
package com.practice;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class UserInterface extends Applet{
JPanel menuPanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel savePanel = new JPanel();
ImageIcon saveIcon = new ImageIcon("workspace/images/toolbarButtongraphics/general/Save24");
JButton saveButton = new JButton("Save", saveIcon);
public UserInterface(){
savePanel.add(saveButton);
setLayout(new BorderLayout());
add(menuPanel, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
add(savePanel, BorderLayout.SOUTH);
} // end UserInterface classThanks for the explanation and example. At first, I didn't understand what you were getting at, but after reading "Using Top-Level Containers" and "How to Use Root Panes" java tutorials it made much more sense. Unfortunately, the books I've read up to this point, did not cover those topics at all. The books simply stated that the first step in creating a Swing gui was to extend the JFrame, or JApplet, or etc.
Unfortunately, my original problem persists. I continue to get compile-time errors such as:
TestUserInterface.java:5: cannot find symbol
symbol: class UserInterface
location: class projects.web.TestUserInterface
UserInterface ui = new UserInterface(); Anyone know why?
Both the classes are in the same named packaged. Below is my code:
package projects.web;
import java.awt.*;
import javax.swing.*;
public class UserInterface extends JFrame{
JPanel menuPanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel selectionPanel = new JPanel();
JButton save = new JButton("Save");
JButton addFiles = new JButton("Add");
public UserInterface(){
super("File Upload");
setSize(500, 500);
menuPanel.add(addFiles);
selectionPanel.add(save);
setLayout(new BorderLayout());
add(menuPanel, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
add(selectionPanel, BorderLayout.SOUTH);
} // end constructor
} // end UserInterface class
package projects.web;
public class TestUserInterface{
public static void main(String[] args){
UserInterface ui = new UserInterface();
} // end TestUserInterface class -
Cannot find class in same package but in different file
I have following two source files. Both the file has same package statement as below
package java.buron.doeacc ;
But whenever i try to compile File: shoepolish.java ( mainfram.java compiled succesfully before)
following error message appear
..\..\buron\doeacc\shoepolish.java:12: cannot resolve symbol
symbol : class MainFrame
location: class java.buron.doeacc.shoepolish
MainFrame mainFrame = new MainFrame();
^
..\..\buron\doeacc\shoepolish.java:12: cannot resolve symbol
symbol : class MainFrame
location: class java.buron.doeacc.shoepolish
MainFrame mainFrame = new MainFrame();
^
Please tell me What is the problem and how it can be solved.
why cannot find the class that are in same package.
I have JDK 1.3
FOLLOWING ARE THE TWO FILES
File: mainframe.java/////////////////////////////////////////////////////////////////////
package java.buron.doeacc ;
import javax.swing.* ;
import java.awt.*;
import java.awt.event.* ;
class MainFrame extends JFrame
private final String APP_NAME = "Shoe Polish";
// constructor
public MainFrame()
super("Shoe Polish");
setSize(500, 500);
setVisible(true);
File : shoepolish.java
package java.buron.doeacc ;
import java.io.* ;
import java.buron.doeacc.* ;
class shoepolish
public static void main(String args[])
MainFrame mainFrame = new MainFrame();The javac compiler uses the Classpath to find classes. If your directory structure is c:\myjava\buron\doeacc (for example) then you need to have c:\myjava in the Classpath when you compile. For example javac -classpath c:\myjava MainFrame.java
-
How do I find classes in a package
Hi,
I am working on an automation tool which is to be used for testing public API's in our product. The tool is supposed to work this way:
1. A developer of the API adds a new java file containing the test code for the API in a particular package (which the tool defines). Then he compiles and places the stuff in a jar. There is also a driver class in this same package path defined by the test tool. E.g.
driver class: com.aaa.bbb.DriverClass
new API test file: com.aaa.bbb.TestFile
Now the driver file's job is to find out all the other classes in this particular package and then do some processing. When I tried doing the getPackage() on this driver class to find out about the package I got back a null.
Question 1: How can I get the package for a particular class (An ugly way to do this would be to strip it out from the classname)
Question 2: How can I find out what other classes are there in a package?
Thanks in advance on this.
Nikhil Singhal
You can also send me mails at
[email protected]hai
i have the same problem to finding the classes in package...
in my case i know the jars name and i have loaded
classes using code
ResourceBundle bundle = ResourceBundle.getBundle("source\\ClasssPath");
StringTokenizer stToke = new StringTokenizer(bundle.getString("ClassPath"),";");
String temp;
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
while(stToke.hasMoreTokens())
temp = stToke.nextToken().trim();
if(temp.endsWith(".jar"))
JarFile jar = new JarFile(new File(temp));
Enumeration en = jar.entries();
String pathStr;
while(en.hasMoreElements())
pathStr = en.nextElement().toString();
System.out.println("pathStr ="+pathStr);
if(pathStr.endsWith(".class"))
System.out.println( classLoader.getResource(pathStr));
System.out.println(classLoader.loadClass(pathStr.substring(0,pathStr.indexOf(".class")).replace('/','.').trim()));
else classLoader.loadClass(temp);
here i am getting the classes in that package using code
String[] filLis = new File("//it//sella//converter//ptlf//startup//").list();
int length = filLis.length;
while(length-- >0)
System.out.println(">"+filLis[length]);
but its returnign the class when this classes in locale folder(i.e)its not getting the classes in loaded memory...
so how to retrieve the class files names using package structure name...
(i am having more then 20 jars files, inthat inside the jar samepackage structue may appear in more then one jars )
pls help me in this field..
Thanx -
Compilation problem -- classes in same package
I feel like I should know the answer to this, but since I can't figure it out, I'll ask it anyway. I have the following three classes, all in the same package. The Dog class compiles just fine. As for the other two, the compiler insists it "cannot find symbol" when it comes to Dog. Can anyone help?
package com.example;
public class Dog {
private String breed;
public Dog(String breed) {
this.breed = breed;
public String getBreed() {
return breed;
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ListenerTester extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test context attributes set by listener<br>");
out.println("<br>");
Dog dog = (Dog) getServletContext().getAttribute("dog");
out.println("Dog's breed is: " + dog.getBreed());
package com.example;
import javax.servlet.*;
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);
public void contextDestroyed(ServletContextEvent event) {
}Ok, your .java files and .class files are in the correct location.
What you should have done is:
1.) Go to C:\tomcat\web-apps\listenerTest\WEB-INF\classes
2.) compile your program using "javac -cp c:\tomcat\lib\servlet-api.jar;. com/example/*.java" (note the "." on the classpath, which tells java to include the current directory on its classpath).
It would also be a good idea to have a sparate tree for the sources and the classes.
And to develop your application independently from the deployment target.
But those are semi-advanced topics that you could ignore for now.
Edit: and again: there's nothing wrong with having classpath trouble when starting Java. But it means that you still have to learn some basics, before you should attempt any J2EE development. Seriously, don't do that yet. -
How to Extend Java class in UI Module
Hi All,
I am trying to make z for one of the java file GenericSearchDynamicContent.java in the standard package com.sap.wec.app.common.module.catalog.ui.utils. Can anyone please let me know what are steps do i need to follow to extend the same java file in our customer namespace and make use of it.
Thanks in Advance.
Regards,
Rahul.Hi Rahul,
It's upto you whether you want to keep seprate packages for custom changes (then create your own package and keep all the Z class under this) or create a Z_GenericSearchDynamicContent.java and extend this class by GenericSearchDynamicContent.java.
After doing this you can make your custom changes accordingly and you have to also make the changes in *config.xml (replace this entry GenericSearchDynamicContent by Z_GenericSearchDynamicContent.java.) files as mapping is done there.
Could you please let me know if you need any further information.
Thanks,
Hamendra -
Java file cannot access other class in same package???????
I have written a bean as follows-------
package CustTags;
public class TomMovieBean
private String movieName;
private String movieDirector;
public void setmovieName(String movieName)
this.movieName = movieName;
public String getmovieName()
return this.movieName;
public void setmovieDirector(String movieDirector)
this.movieDirector = movieDirector;
public String getmovieDirector()
return this.movieDirector;
Now i am writing a tag handler for my JSP custom tag as follows----------
package CustTags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class Dynamic extends TagSupport
private List movieList;
public void setmovieList(List movieList)
this.movieList = movieList;
public int doStartTag() throws JspException
Iterator iterator = movieList.iterator();
TomMovieBean TMBObj = null;
try
JspWriter out = pageContext.getOut();
while(iterator.hasNext())
TMBobj = (TomMovieBean)iterator.next();
String movieName = (String)TMBObj.getmovieName();
String movieDirector = (String)TMBObj.getmovieDirector();
out.println(movieName+"...."+movieDirector+"<br>");
}catch(Exception ex)
throw new JspException("Error in doStartTag()");
return SKIP_BODY;
Now when i compile Dynamic.java it shows foll. errors
Dynamic.java:19: cannot resolve symbol
symbol : class TomMovieBean
location: class CustTags.Dynamic
TomMovieBean TMBObj = null;
^
Dynamic.java:27: cannot resolve symbol
symbol : variable TMBobj
location: class CustTags.Dynamic
TMBobj = (TomMovieBean)iterator.next();
^
Dynamic.java:27: cannot resolve symbol
symbol : class TomMovieBean
location: class CustTags.Dynamic
TMBobj = (TomMovieBean)iterator.next();
^
3 errors
I am unable to comprehend why it can't recognize TomMovieBean despite the fact that its a public class and in the same package as that of Dynamic.javaIs your classpath set correctly? I.e. does it point to the directory containing the CustTags directory?
BTW, by convention, package names are written in lower case. -
Preferences API: how to use different prefs in same package?
I have some JFrames representing different modules (applications) in the same package (a.b.c.gui.*).
Each frame stores preferences entries "x", "y", "width", "height" for their frame bounds. of course, this does not work as these values are all stored in the same preferences package node. is there another way to still have these classes in the same package and store each class'es "x", etc. as separate preferences entries?found it out myself:
private Preferences prefs = Preferences.userRoot().node(MyClass.class.getName()); -
Using class of same package in another
Hi,
I have posted this message before but I feel I did not provide enough information.
I have a directory called "beans" where I have two classes, A and B which are both part of the "beans" package. I am trying to use a method of a class A from class B, but when I compile class B I get the following error:
ArticleBean.java:140: cannot resolve symbol
symbol : class GetCurrentEditionBean
location: class beans.AddArticleBean
GetCurrentEditionBean current_edition = new GetCurrentEditionBean();
^
Here the actual code:
// GetCurrentEditionBean
package beans;
import java.io.*;
public class GetCurrentEditionBean {
// constructor
GetCurrentEditionBean () { /* empty */ }
public String process() { /* do something... */ }
// AddArticleBean
package beans;
public class AddArticleBean {
public String add() {
GetCurrentEditionBean current_edition = new GetCurrentEditionBean();
String result = current_edition.process();
}Both the classes are in the same directory, I wonder what I could be doing wrong here?
Thanks,
JunaidYou do not need to import a class if it is in the same package as the class trying to use it. You do need to have an environment in which the compiler can find the class.
Let's say your directory structure is c:\myjava\beans. When you compile AddArticleBean, you must have c:\myjava in your classpath, not c:\myjava\beans. It might work if you "cd c:\myjava" and compile with "javac beans\AddArticleBean.java" -
AS3.0: How to extend a class that extends MovieClip
When I try to set the base class of a library symbol to a
class that doesn't DIRECTLY extend MovieClip, but instead extends
another class that DOES extend MovieClip, it's disallowed, saying,
"The class 'Whatever' must subclass 'flash.display.MovieClip' since
it is linked..."
Is this just a validation bug in the property windows, only
checking one class deep into the inheritance hierarchy? Because the
specified class does extend MovieClip, just two levels in instead
of one. Is there a fix for this? Or must library symbols always
directly extend MovieClip? If so, why?Which classes from flash.display have you imported. Just
because a class extends another doesn't mean that it inherits all
of its properties. Saying a class extends sprite doesn't give you
access to all of it's properties, members and display unless you
first import flash.display.* or flash.display.Sprite. If you aren't
already importing flash.display.*, try importing
flash.display.MovieClip and see what happens... -
How to Access a class outside a package which is friendly to the package
Hi All,
i hav a class which has friendly scope in package X. how do i access it from a class in package Y without declaring it as public.
Rgds,Hmm. If it is friendly in X, you can access it only from X. There is no way you can access it from Y.
Maybe you are looking for
-
Backing up VHS video data 8600 RCA ports
Hi all has anyone been successful on transferring their lovely VHS home video collection via the 8600 RCA ports on back? I'm in the process of trying this but wanted feedback before attempting this. I heard it's a very long and tedious process. Thank
-
Can Any one helpme with logic for fortnight
Hi All, I need the logic for Fortnight of the year for drill in reports . Can any one help me regarding this. Thanks in Advance
-
I cannot update my ipod 3G (32 gb) NO ONE KNOWS HOW TO FIX THIS.
"iTunes could not contact the iPod software update server because you are not connected to the Internet. Make sure your Internet connection is active and try again" This has been the plight of new ipod. I bought an iPod 3G (32 gb) used. It hadnt be
-
Embedding Flex applications inside a portlet question
I'm trying to embed a Flex application within a Java portlet on my WLP 10.3.2 portal. I'm able to display the Flex app in the portlet but now I'm trying to access data. Does anyone know of any documents that cover how to access Flex data from a portl
-
Trouble passing Value from MenuEvent
In the code that follows I can't figurre out how to get a value for e.label for the lone Menu elements, i.e. "menuItems.push({label:"Model-A"});". Thanks in advance for any help. private function init():void{ var menuItems:Array = new Array();