1

I'm really new to Java and have to constantly formulate and solve questions. But the only thing I'm not able to do at all on my own is to import packages to Java. I've looked through answers to many similar questions, but they either don't work in my case or very unclear.

The problem is very general: I have package in the directory X, containing jar file X-4.9.jar, source files in X/src subdirectory and usual other directories. I set up CLASSPATH to directory Y by using command prompt, such that Y contains X. I need to figure out how to import it in my public class. As I understand I will need to write:

1) import Name1.Name2...NameK.*; Question: how to find out Name1, Name2..?

2) Do I need to do something else? I use NetBeans IDE and don't have the menu option to add library or jar to the project an my class is not an application. I also use Maven for the first build of each class I write (basically write a library), but then use NetBeans as it's faster.

I need the simplest guaranteed to work way to accomplish the goal, even if it's ugly. The link to clear explanation on how the algorithm can be calculated will be also appreciated.

EDIT: Suppose I need to import CSSBox package. Do I write in my import statement cssbox.* or net.sf.cssbox.? Why not some.hard.to.find.package.? By Name1, Name2 I indeed mean names, not the packages/classes it refers to.

Allex
  • 11
  • 2

1 Answers1

0

Read the answers to this question: Reflection

This will tell you how close you can get to finding out Name1 and Name2, but you simply can't do it. Those import statements are thrown away before the class is compiled to an object file.

Read this question for a detailed explanation: Finding imports with reflection

Community
  • 1
  • 1
Matt C
  • 4,470
  • 5
  • 26
  • 44
  • Thank you. I'm not looking for particular methods within imports, It's fine to import everything. But the problem is that the compiler just can't find a package at all. I need to help it, but don't know how and where it's looking for packages to import. – Allex May 29 '15 at 16:33
  • I understand you don't need particular methods, rather libraries or packages that are imported. You can not do this because those import statements are thrown away. – Matt C May 29 '15 at 17:09