How to Use Classes from .Jar Files

How to use classes from .jar files?

Not every jar file is executable.

Now, you need to import the classes, which are there under the jar, in your java file. For example,

import org.xml.sax.SAXException;

If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.

And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,

java -jar my_program.jar

you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.

Use a class from a jar file in VSCode

Here's the solution:

1.Use the command javac Ler.java to compile Ler.java;

2.Type the command jar --create --file Ler.jar Ler.class to generate Ler.jar;

Attention: If Ler.java is under the subfolder, please enter it then compile and generate .jar.

Sample Image

3.Add Ler.jar to Referenced Libraries;

4.Code like Ler.function().

Sample Image

Is there a way to access a class from another .jar file in java

You're actually using classes (import) from other .jar's every time your write java code - using libs from the jdk. As a side note, in java, the convention is to use capitals for a class name, so you may want to use: GetSomething.java (.class), or simply A, B, C)

Each .jar will have multiple packages, so the only thing you have to do is:

import C

or, if the class C is in a package:

import package.x.y.C

The only constraing is to have both .jars in the classpath

You may find this tutorial to guide you (found it by googling for: java learn packages and classes)

Can .class files be imported instead of .jar files?

Eclipse allows you to add a class file folder to your classpath:

  1. Right-click on a Java project
  2. Select Properties
  3. Select Java Build Path
  4. Click Add External Class Folder... in the tabs on the right

How to get names of classes inside a jar file?

Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or (b) you can use a library that does this for you.

Option (a): Scanning JAR files manually

In this option, we'll fill classNames with the list of all Java classes contained inside a jar file at /path/to/jar/file.jar.

List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
// This ZipEntry represents a class. Now, what class does it represent?
String className = entry.getName().replace('/', '.'); // including ".class"
classNames.add(className.substring(0, className.length() - ".class".length()));
}
}

Option (b): Using specialized reflections libraries

Guava

Guava has had ClassPath since at least 14.0, which I have used and liked. One nice thing about ClassPath is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.

ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader());
for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) {
// Do stuff with classes here...
}

Reflections

I haven't personally used the Reflections library, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by any JAR file, which may also be useful for your application.

Reflections reflections = new Reflections("my.project.prefix");

Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);

Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);


Related Topics



Leave a reply



Submit