-1

I want to use the classes (that are .java files) which I designed separately in java and I want to add them into java library to use them in other classes with "import" keyword. How can I do this ?

Also, I should explain that I copied the classes folder in the zip file which its name is src.zip at C:\Program Files\Java\jdk1.7.0_25 but when I use the "import" keyword in the IDE it seems that there is not any folder with that name in java library.

David Starkey
  • 1,733
  • 3
  • 28
  • 46

2 Answers2

0

I am not sure if I understand this correctly but you need to make a jar file from your library project and then place that jar file into the project which wants to use that library. How do you do this depends mainly on the IDE which you are using.

Add library in NetBeans

Add library in Eclipse

Add library in IntelliJ

Community
  • 1
  • 1
Petr Mensik
  • 24,455
  • 13
  • 84
  • 111
0

What needs to be available to Java are the object (aka .class files). So, you can do 2 things:

  1. Locate the directory where you .class files are and add it to the CLASSPATH (either by changing the environment variable, or by specifying switch -cp to java). Be careful with one thing: if you have a class MyClass in package stackoverflow.com, in the directory structure you will see something like .../com/stackoverflow/MyClass.class. The base folder to include in CLASSPATH is .../com, not .../com/stackoverflow .

  2. Packaging your classes into a .jar file (pretty much a .zip, but with a different extension), and add it to the CLASSPATH as in 1. Note that n this case you use the name of the .jar, not the name of the directory it is in.

src.zip does not contain classes, but source (.java) files. That's why its extension is not .jar, and also why you don't see any effect.

Mario Rossi
  • 7,231
  • 21
  • 36