[英]how to compile and run java in linux with 3rd party jar and my own jar
I export my own project into a jar, and this project needs two 3rd-party jars, an extra TestMyJar.class is used to test my project, how to do this? 我将自己的项目导出到jar中,该项目需要两个3rd-party jars,一个额外的TestMyJar.class用于测试我的项目,该怎么做? I have tried several methods but no luck. 我尝试了几种方法,但是没有运气。 To be more specific, this is my jar: a class that only delivers hello world message a url. 更具体地说,这是我的jar:一个仅向hello world消息传递url的类。 I export this class code into a helloworld.jar 我将此类代码导出到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";
}
}
}
It requires dom4j and httpclient. 它需要dom4j和httpclient。 This is my TestMyJar.class: 这是我的TestMyJar.class:
package httptest
public class TestMyJar {
public static void main(String[] args) {
HttpConnection connection= new HttpConnection();
}
}
Now I have three jar: helloworld.jar, commons-httpclient-3.1.jar, dom4j-1.6.1.jar, and a class: TestMyJar.java. 现在,我有三个jar:helloworld.jar,commons-httpclient-3.1.jar,dom4j-1.6.1.jar和一个类:TestMyJar.java。 How can I compile and run TestMyJar.java? 如何编译和运行TestMyJar.java? I have tried with javac and java, but it is all something cannot be found. 我已经尝试使用javac和java,但是所有这些都找不到。
Thanks! 谢谢!
you can include as many jars as you wish with the command 您可以在命令中包含任意数量的jar
javac MyClass.java -cp jar1 jar2 jar3
java -cp jar1 jar2 jar3 MyClass
On Windows, you could just run your main class with command like: 在Windows上,您可以使用以下命令运行主类:
java c:/lib/helloworld.jar;c:/lib/commons-httpclient-3.1.jar;c:/lib/dom4j-1.6.1.jar httptest.TestMyJar
While on Linux, use: 在Linux上,请使用:
java /lib/helloworld.jar:/lib/commons-httpclient-3.1.jar;/lib/dom4j-1.6.1.jar httptest.TestMyJar
Where httptest is your package name and TestMyJar is your class with main method in it. 其中httptest是您的程序包名称,TestMyJar是您的类,其中包含main方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.