samedi 9 mai 2015

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? 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. I export this class code into a 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. This is my 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. How can I compile and run TestMyJar.java? I have tried with javac and java, but it is all something cannot be found.

Thanks!

Aucun commentaire:

Enregistrer un commentaire