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

时间:2015-05-09 13:21:09

标签: java linux jar javac

我将自己的项目导出到一个jar中,这个项目需要两个第三方jar,一个额外的TestMyJar.class用于测试我的项目,如何做到这一点?我尝试了几种方法,但没有运气。更具体地说,这是我的jar:一个只传递hello world消息的类。我将这个类代码导出到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,但是找不到它。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用命令

包含任意数量的罐子
javac MyClass.java -cp jar1 jar2 jar3

java -cp jar1 jar2 jar3 MyClass