This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

package information from string

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you have a string that contains a class name (not fully qualified, i.e. Test instead of com.company.Test) can you retrieve the package name ?
I have tried:
String = "test";
Class test = Class.forName(string);
Package package = test.getPackage();
System.out.println(package.getName());
This causes a NoClassDefFoundError.
If String was "com.company.Test"
The output would be:
com.company
Any help would be appreciated.
Regards,
Damian.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Damian
I wonder why you would want to do something like that? Here, Class.forName() can only accept "fully qualified" class names. So if Test is in com.package the it MUST be loaded as com.package.Test rather than just Test....
Even if we have imported like,
import com.package.*;
in the program it would not work for Class.forName() as Class.forName() would use ClassLoader to load the class that looks up in CLASSPATH...
Thanks!
Maulin
 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin,
Thanks for your reply.
My situation is that I have a FileChooser where I choose the path of a particular class file name. So I was hoping there was some mechanism to find the package from an not fully qualified class name.
I know that I could access the path as well as the filename of the class I choose from the FileChoser. I could then find the package name from the path
if I knew the start of the package.
This seems an ackward way to complete the task so I was wondering if there was a better solution.
Regards,
Damian.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really do need the full class name. Consider the case of the "Date" class. It could resolve to "java.util.Date" or "java.sql.Date" or, perhaps, even to a user-defined class. How is the JVM to know which is the intended class? No, you need the full class name.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[damian]: I know that I could access the path as well as the filename of the class I choose from the FileChoser. I could then find the package name from the path if I knew the start of the package.
It may be awkward, but this sounds like your best bet. Let's say the complete path is
/foo/bar/baz/a/b/c/MyClass.class
I'd try these in succession:
Class.forName("MyClass")
Class.forName("c.MyClass")
Class.forName("b.c.MyClass")
Class.forName("a.b.c.MyClass")
Class.forName("baz.a.b.c.MyClass")
Class.forName("bar.baz.a.b.c.MyClass")
Class.forName("foo.bar.baz.a.b.c.MyClass")
The first one of these that returns != null, that's your class.
 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
[damian]: I know that I could access the path as well as the filename of the class I choose from the FileChoser. I could then find the package name from the path if I knew the start of the package.
It may be awkward, but this sounds like your best bet. Let's say the complete path is
/foo/bar/baz/a/b/c/MyClass.class
I'd try these in succession:
Class.forName("MyClass")
Class.forName("c.MyClass")
Class.forName("b.c.MyClass")
Class.forName("a.b.c.MyClass")
Class.forName("baz.a.b.c.MyClass")
Class.forName("bar.baz.a.b.c.MyClass")
Class.forName("foo.bar.baz.a.b.c.MyClass")
The first one of these that returns != null, that's your class.


Or... you can write your own class loader (or use URLClassLoader), and load the class file selected by the user. Once this is loaded, you can use reflection to find the package the class is in, or do whatever else you'd want to do with it.
-GB.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!