如何使用第三方罐子和我自己的罐子在Linux中编译和运行Java



我将自己的项目导出到一个罐子中,并且该项目需要两个第三方罐子,这是一个额外的testmyjar.class。类用于测试我的项目,该怎么做?我尝试了几种方法,但没有运气。更具体地说,这是我的罐子:只能传达Hello World信息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();
    }
}

现在,我有三个罐子:helloworld.jar,commons-httpclient-3.1.jar,dom4j-1.6.1.jar和一个类:testmyjar.java。如何编译并运行testmyjar.java?我已经尝试了Javac和Java,但这都是找不到的。

谢谢!

您可以随意包含命令

的任意数量
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是您的班级。

最新更新