如何在使用另一个jar文件时创建jar文件?

时间:2015-09-14 09:57:22

标签: java jar

我正在尝试编写应用程序。在应用程序中我使用另一个jar文件(opr.jar)。在opr.jar(d:\ opr.jar)我有一个接口Operator:

public class Company
    {
        public int companyId { get; set; }
        public string name { get; set; }
        public string telephone { get; set; }
        public string regNumber { get; set; }
        public virtual IList<Asn> asns { get; set; }
        public virtual IList<Contact> contacts { get; set; }
    }
    public class Contact
    {
        public int contactId { get; set; }
        public int companyId { get; set; }
        public Company company { get; set; }
        public string name { get; set; }
        public string telephone { get; set; }
    }
public class Asn
    {
        public int asnId { get; set; }
        public int companyId { get; set; }
        public Company company { get; set; }
        public bool userBehalf { get; set; }
        public bool something { get; set; }
    }

我也有类Plus(d:\ math \ Plus)实现操作员界面:

package operatorAPI;

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

在主程序中(d:\ source \ main \ Sum)我加载类Plus并使用操作员界面:

package math;

import operatorAPI.*;

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

我使用命令行来编译和运行程序。我的应用程序工作正常,但是当我尝试将我的主程序(source.main.Sum)作为名为app.jar的可执行jar文件并运行它时,我得到此异常:

package source.main;

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


public class Sum
{
    public static void main(String[] args) throws Exception
    {
        File operatorFile = new File("D:\\");
        URL operatorFilePath = operatorFile.toURL();
        URL[] operatorFilePaths = new URL[]{operatorFilePath};
        ClassLoader operatorsLoader = new URLClassLoader(operatorFilePaths);
        Class currentClass = operatorsLoader.loadClass("math.Plus");

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

我像这样制作并运行jar 0file:

NoClassDefFoundError : operatorAPI/Operator

有谁能告诉我为什么会这样?

0 个答案:

没有答案