简体   繁体   English

如何在我的程序中加载Jar文件?

[英]How to load Jar file in my program?

I'm trying to write a program that load classes and jar files .My app handle the plus operator .For this I have 2 class and one jar file : 我正在尝试编写一个加载类和jar文件的程序。我的应用程序处理plus运算符。为此,我有2个类和一个jar文件:

1- Operator interface jar file in directory D:\\operatorAPI (Operator interface is a jar file) 1-目录D:\\ operatorAPI中的操作员接口jar文件(操作员接口是jar文件)

package OperatorAPI;

public interface Operator
{
    int calculate(int num1 , int num2);
}

2-class Plus in directory (D:\\operators) 目录中的2类Plus(D:\\ operators)

package operators;

import OperatorAPI.*;

public class Plus implements Operator
{
    public int calculate(int num1 , int num2)
    {
        return num1 + num2;
    }
}

3- Main class in directory D:\\source\\main : 3-目录D:\\ source \\ main中的主类:

package source.main;

import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import operatorAPI.*;

public class Main
{
    public static void main(String[] args)
    {
        File file = new File("D:\\");
        URI uri = file.toURI();
        URL[] urls = new URL[] {uri.toURL}
        ClassLoader classloader = new URLClassLoader (urls);
        Class clazz = classloader.loadClass("operator.Plus");

        Operator instance = (Operator) clazz.newInstance();
        int output = instance.calculate(10,20);
        System.out.println("The result is :" + output);
    }
}

But when I run my Program in command line I get this exception : Exception in thread "main" java.lang.NoClassDefFoundError: operatorAPI/Operator 但是,当我在命令行中运行程序时,出现此异常:线程“ main”中的异常java.lang.NoClassDefFoundError:operatorAPI / Operator

I think because Operator interface is a jar file I should load it to my program But I don't know How to do this . 我认为,因为Operator Interface是一个jar文件,我应该将其加载到程序中,但是我不知道如何执行此操作。 Can anyone help me? 谁能帮我?

Currently you're only using D:\\ in your classpath: 当前,您仅在类路径中使用D:\\

File file = new File("D:\\");
URI uri = file.toURI();
URL[] urls = new URL[] {uri.toURL}
ClassLoader classloader = new URLClassLoader (urls);

If you want to use a jar file, you should specify that: 如果要使用jar文件,则应指定:

File file = new File("D:\\operator.jar");

If that's all you need your new classloader to load, that should be the only change you need. 如果这是你所需要新的类加载器加载,这应该是你唯一需要的变化。 You might want to make this a command-line argument, mind you. 请注意,您可能希望将此作为命令行参数。

As you use Operator within your "driver" class, you'll need that in the classpath you run with though. 在“ driver”类中使用Operator ,您将需要在运行它的类路径中使用它。 For example: 例如:

java -cp operatorAPI.jar;. source.main.Main

You shouldn't need to add operator.jar to the classpath when compiling though... if the idea is to write a "pluggable" system, others should be able to add plugins without you knowing anything about them at compile time. 不过, 编译时无需在类路径中添加operator.jar ...如果您的想法是编写“可插拔”系统,则其他人应该能够添加插件而无需在编译时不了解插件。 Both your code and the plugins should just know about the shared interface. 您的代码和插件都应该只知道共享接口。

What you are trying to do is to load the operators (Plus etc.) dynamically. 您尝试做的是动态加载运算符(加号等)。 If you have the operator classes under 'D:\\', the way you are loading is correct. 如果在“ D:\\”下有运算符类,则装载方式正确。 However, your interface 'Operator' in the JAR itself is not loaded. 但是,JAR本身的接口“ Operator”未加载。 The Plus operator depends on this interface and expects it to be loaded. Plus运算符取决于此接口,并期望将其加载。 Adding the operator.jar file to your class path should solve the problem. 将operator.jar文件添加到您的类路径应该可以解决该问题。

If you want to load the 'Operator.jar' also dynamically, update the urls passing to the URLClassLoader. 如果要动态加载“ Operator.jar”,请更新传递给URLClassLoader的URL。

URL[] urls = new URL[] {uri.toURL, new File("PathtoOperator.jar").toURI().toURL() }

This should resolve the error you are receiving. 这样可以解决您收到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM