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
Similar Messages
-
How do I find class name I should import for java method name
If I have the class name I know how to look in java docs list for methods.
If I have a method name but don't know the class,
how do I find class name I should import to use the java method name?I'm not sure the other reply answered your question. If you truly don't know the class name, one approach would be a Google search. Another approach works if you have an IDE. Eclipse, for example, can find the class for you (assuming it's on your classpath) & automatically import it for you. Ctrl-Shift-O normally does the trick.
-
How to find classes in the package if package path
in java i want to find classes,Interface,Exception names which are presant
in the package..
for example
java.sql; if i give this it should return
class name
Date
Time
DrivarManager
pls helpme in this fielddownload the jdk documentation or view it here
http://java.sun.com/j2se/1.3/docs/api/index.html
The exact problem is i hava set the path to my application that path contains several jar's(contains classes,Interfaces) and classes with package structure,
now my need is if i run the application ,the application
should find out the classes and Interfaces for given package structure which are present in classpath...
for this i have to load all the classes to memory(using classes loader) . then find out the classes which are present in memory...
or
any other inbuilt method is there in java when i give path structure it find and give classes which are in class path..
for ex..
if i set path to jdk
then if i give the path like java.lang
it should give me the out put as
String (or) String.class
Double
Cloneable
(classes & interface in that package) -
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
-
I'm new to java. What's the best way to find all classes in a standard package (e.g. java.awt). I'm trying to create a list for classes in a package in java.
thanksI guess you're talking about finding them in a running program (like scanning).
Well, I'm searching a solution for a similar problem.
Unfortunately, java classes are loaded on demand. That is, if you're not using kung.foo.MyClass in your code, its not known by the vm, so you can't find it using VM calls.
Since this behavior is the same for packages, you can test this behavior if you're scanning packages using Package.getPackages():
public class PackageTester {
// drops the package info on screen.
public static void listAll (String message , Package [] data) {
System.out.println ( ) ;
System.out.println ( message) ;
System.out.println ( "Amount of passed classes : "+data.length) ;
for (int i = 0 ; i < data.length; i++)
System.out.println ( data.toString()) ;
public static void main (Strinbg [] args) {
// get a list of all known packages
Package [] state1 = Package.getPackages() ;
// load a yet unknown class
MyClass kong.foo.myClass = new kong.foo.MyClass () ;
// reload all packages
Package [] state2 = Package.getPackages() ;
// print lists to screen
listAll ("packages before loading MyClass" , state1) ;
listAll ("packages after loading MyClass" , state2) ;
Didnt check, but the code should compile.
This means that, if you want to scan for classes in the vm, you'll have to say the vm which classes you're scanning for. Of course, this is nonsense.
The most complete way is to scan all files starting at the classpath-entries, and looking for .class-files in directories and .jar/.zip-files. This is much work to do, but it seems to be the only useful solution to solve this nasty problem. You could use tools, which surely are available; but its not an elegant solution.
Sorry. -
How do I find and downgrade a package to a specific version?
I'm looking to download my SVN package to 1.4, how can I find the package and downgrade it?
Thank you!I did already. I obviously googled before asking
I tried finding my package there but to no avail. So I'm now stuck with creating my own PKGBUILD. Anyone has an experience with this? Or have any idea where I can find a svn 1.4 pkgbuild? -
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 -
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. -
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.
-
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 errorsdid you compile the other class already? donnow if ithahahaha!!!!!
ok include the import statement
import pmtools.ConnectDB;
in the second class and than compile.. -
Import the classes in the package java.util
I haven't taken a class in two years so I have ideas but am unsure of myself, and I most likely am way off base, but is this how you import the classes in the package java.util
import java.util.*;
I would appreciate anyone's help. I have a lot of questions.
Thanks,
rpMy assignment is below so that you will know exactly what I am asking. I am being asked to create an instance of LinkedList. I know that this is a part of the java.util. So when you create an instance of LinkedList would that be similar to instance variables? or am I misunderstanding something. I have several ideas, so I will start with the first one.
LinkList();
I really appreciate your taking the time to answer my questions to help me to rebuild my confidence in doing this.
Thanks,
// 2. import the classes in the package java.util
public class MyProgram
public static void main(String [] args)
// 3. Create an instance of LinkedList
// 4. add 5 entries to the list so that it contains
// Ann, Bart, Carl, Dirk, Zak
// 5. display the list on one line using a loop and ADT list methods
System.out.println("\n\n3. ");
// 6. display the list using one println
System.out.println("\n\n4. ");
// 7. create a ListIterator object for the list
// 8. display the list on one line using the iterator
System.out.println("\n\n6. ");
// 9. restore the iterator to the first item in the list
// 10. retrieve and display the first item in the iteration,
// then advance the iterator to the next item
System.out.println("\n\n8. ");
// 11. advance the iterator to the end of the list without displaying anything;
// do not use the length of the list
// 12. display the last item in the list
System.out.println("\n\n10. ");
// 13. move back to the beginning of the list without displaying anything;
// do not use the length of the list
// 14. display the first item in the list
System.out.println("\n\n12. ");
// 15. advance the iterator to the end of the list without displaying anything;
// 16. advance the iterator - what happens?
System.out.println("\n\n14. ");
// 17. advance the iterator within a try block; catch the exception, display a message,,
// and set the iterator back to the beginning of the list.
System.out.println("\n\n15. ");
// 18. what does nextIndex and previousIndex return?
System.out.println("\n\n16. ");
// 19. move the iterator to the third item in the list; if you were to
// execute next(), the third item would be returned and the iterator
// would advance to the 4th item.
System.out.println("\n\n17. ");
// 20. add the string "New" to the list; where is it inserted relative to
// the current item in the iteration?
System.out.println("\n\n18. ");
// 21. remove the current item; what happens?
System.out.println("\n\n19. ");
// 22. display the current item in the list without advancing the iteration
// in 2 ways, as follows:
// 22A - using only iterator methods
System.out.println("\n\n20A. ");
// 22B using an iterator method and list methods
System.out.println("\n\n20B. ");
// 23. advance the iterator and remove the current item; which one is removed?
System.out.println("\n\n21. ");
// 24. move the iterator back and remove the current item; which one is removed?
System.out.println("\n\n22. ");
// 25. move the iterator to the first item in the list and change it to "First"
System.out.println("\n\n23. ");
} // end main
} // end MyProgram -
Find classes in ear from a outside class
How can we find classes located in a ear from a class that is located outside the
archive? Is it possible?
Regards
NickWell, the purpose of an ear file is to bundle your entire application into one
file.
If you have a class B that needs to use class A which just happens to be in your
ear file, I would think that this file resides in a completely different Application
on the server.
If that's the case, then you definitely need to place class A which is in your
ear file into the classpath of the application which contains class B, or into
the same ear file of class B's application.
If not and both are part of the same application, then why isn't class B included
in the ear file to begin with?
Hope this helps..
"Nick" <[email protected]> wrote:
>
How can we find classes located in a ear from a class that is located
outside the
archive? Is it possible?
Regards
Nick -
How to find classes in a particular package?
Does anyone know how in Java to find out what classes belong to a particular package? Or to find out what classes inherit from a particular class?
The reason why I'm asking this is that I want to create a BeanInfo property editor for a widget that will give me a list of any classes in a particular package. I know what that package can be and I know what the main class is that everyone should inherit from. I can easily create a beaninfo property editor with a string array of the classes, but I don't want to have to go back and maintain this code everytime I add another class to that package. I would love to use introspection but I haven't found the right commands to use yet.
Anyone have any ideas?
Thanks!Not too difficult, the following snippet is used to gather information about a class. beanClass can be parsed to extract package name. and you ask the class directly about its superClasses and implemented Interfaces.
private Class beanClass = null;
private Class beanSuperClass = null;
private Constructor[] constructors = null;
private Field[] fields = null;
private Method[] methods = null;
private Object beanInstance = null;
public setBeanDetail(Object o) {
beanInstance = o;
beanClass = o.getClass();
beanSuperClass = beanClass.getSuperclass();
constructors = beanClass.getConstructors();
fields = beanClass.getDeclaredFields();
methods = beanClass.getMethods();
}have loads of fun with it.
tj... -
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());} -
How to find (classes,interface,subpackage info) in some package
Plz tell me if any one know how to find (classes,interface,subpackage) in some package
e.g. in java.io
"Above senerio in not concern with java doc
but tell me if an other way i.e by help of programming
i could find some package information"
as we know we find out any "object" classType , methods and fields information with help of "java.lang.reflect" API's
but how to find a package information as provided by jdk is in current useYou can locate the .zip or .jar file where the stuff is, and say either
unzip -l classes.zip
or
jar tvf classes.jar.
So you see the list of the classes in the given package.
Then you can say
javap -classpath classes.zip MyPackage.MyClass
to see the fields and methods of the given class.
Maybe you are looking for
-
Flashing video with composite av cable
Having some difficulty with my composite av cable. It's been malfunctioning with all my apple devices. I am getting sound perfect but the video is rapidly flashing and moving at an up and down pace, like it's "tracking" if you remember the old days
-
Query with char. in column
Hello all I have input ready query, with one char. in the columns (Line of business). When i try to add new row with data, the system puts the data with line of business =#, even when i write it under another value of line of business. Is it not poss
-
Hi, In the near future, we will deploy our engines in cluster. I have some extra questions: We'll implement it this way: 1. Install the Oracle BPEL Process Manager Server on two different machines. 2. Set up a load balancer as the proxy for both Orac
-
Hi, I'm sure this question must have been asked before, but I can't find an answer to it. When I try to create a spatial index I get the following error: CREATE INDEX IDX_ADDR_SPT ERROR at line 1: ORA-29855: error occurred in the execution of ODCIIND
-
Deployment of JSP to Weblogic6.1: Encoding windows-1252" not supported
When i try to deploy my JSP aaplication deployment 'webapp1.deploy' to Weblogic6.1, the following error raised: [weblogic.xml.process.XMLParsingException: The encoding "windows-1252" is not supported. - with nested exception: I have tried to change t