如何使用第三方jar和我自己的jar在linux中编译和运行java

如何使用第三方jar和我自己的jar在linux中编译和运行java,java,linux,jar,javac,Java,Linux,Jar,Javac,我将自己的项目导出到一个jar中,这个项目需要两个第三方jar,一个额外的TestMyJar.class用于测试我的项目,如何做到这一点?我试过几种方法,但没有成功。更具体地说,这是我的jar:一个只提供helloworld消息和url的类。我将这个类代码导出到helloworld.jar中 package com.wow.flow.http.dq; import java.io.BufferedReader; import java.io.IOException; import java.i

我将自己的项目导出到一个jar中,这个项目需要两个第三方jar,一个额外的TestMyJar.class用于测试我的项目,如何做到这一点?我试过几种方法,但没有成功。更具体地说,这是我的jar:一个只提供helloworld消息和url的类。我将这个类代码导出到helloworld.jar中

package com.wow.flow.http.dq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpConnection {

    @SuppressWarnings("deprecation")
    public void client() throws Exception {

        String url = "www.someurl.com"; // sorry if this your registered url, just borrow it as an example
        if (url == null) {
            throw new Exception();
        }

        HttpClient client = new HttpClient();

        PostMethod postMethod = new UTF8PostMethod(url);
        try {
            postMethod.setRequestBody("Hello world");
            int statusCode = client.executeMethod(postMethod);

            if (statusCode == HttpStatus.SC_OK) {

                InputStream responseBody = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(responseBody, "utf-8"));
                String line = reader.readLine();
                while (line != null) {
                    System.out.println(new String(line.getBytes()));
                    line = reader.readLine();
                }
            }

        } catch (HttpException e) {
            // TODO: handle exception
        } catch (IOException e) {
            // TODO: handle exception
        } finally {
            postMethod.releaseConnection();
        }
    }

    // Inner class for UTF-8 support
    public static class UTF8PostMethod extends PostMethod {
        public UTF8PostMethod(String url) {
            super(url);
        }

        @Override
        public String getRequestCharSet() {
            // return super.getRequestCharSet();
            return "UTF-8";
        }
    }

}
它需要dom4j和httpclient。这是我的TestMyJar.class:

package httptest

public class TestMyJar {
    public static void main(String[] args) {
        HttpConnection connection= new HttpConnection();
    }
}
现在我有三个jar:helloworld.jar、commons-httpclient-3.1.jar、dom4j-1.6.1.jar和一个类:TestMyJar.java。如何编译和运行TestMyJar.java?我尝试过javac和java,但都是找不到的东西


谢谢

您可以使用命令包含任意多个jar

javac MyClass.java -cp jar1 jar2 jar3

java -cp jar1 jar2 jar3 MyClass

在Windows上,您可以使用如下命令运行主类:

java c:/lib/helloworld.jar;c:/lib/commons-httpclient-3.1.jar;c:/lib/dom4j-1.6.1.jar httptest.TestMyJar 
在Linux上时,请使用:

java /lib/helloworld.jar:/lib/commons-httpclient-3.1.jar;/lib/dom4j-1.6.1.jar httptest.TestMyJar 

其中httptest是您的包名,TestMyJar是包含main方法的类。

我尝试javac MyClass.java-cp jar1.jar jar2.jar jar3.jar四个文件位于同一目录中,工作目录相同。我得到了以下信息:javac:invalid flag:commons-httpclient-3.1.jar用法:javac use-help获取可能选项的列表hi,我在/lib中放入了3个jar,但是我得到了:[root@flowhttptest]#java/lib/helloworld.jar:/lib/commons-httpclient-3.1.jar:/lib/dom4j-1.6.1.jar httptest.TestMyJar线程“main”中的异常java.lang.NoClassDefFoundError:/lib/helloworld/jar:/lib/commons-httpclient-3/1/jar:/lib/dom4j-1/6/1/jar由以下原因引起:java.lang.ClassNotFoundException:.lib.helloworld.jar:.lib.commons-httpclient-3.1.jar:.lib.dom4j-1.6.1.jar。。。注释中的单词太多,必须忽略它,因为找不到主类:/lib/helloworld.jar:/lib/commons-httpclient-3.1.jar:/lib/dom4j-1.6.1.jar。程序将退出。