gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:58 25 4
gpt4 key购买 nike

我将自己的项目导出为jar,并且该项目需要两个3rd-party jar,额外使用TestMyJar.class来测试我的项目,该怎么做?我尝试了几种方法但没有成功。更具体地说,这是我的 jar:一个仅传递 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();
}
}

现在我有三个 jar:helloworld.jar、commons-httpclient-3.1.jar、dom4j-1.6.1.jar 和一个类:TestMyJar.java。如何编译并运行 TestMyJar.java?我已经尝试过javac和java,但都是找不到东西。

谢谢!

最佳答案

您可以使用命令包含任意数量的 jars

javac MyClass.java -cp jar1 jar2 jar3

java -cp jar1 jar2 jar3 MyClass

关于java - 如何使用第3方jar和我自己的jar在linux中编译和运行java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com