Compile And Run Java Program With External Jar

From Chorke Wiki
Jump to navigation Jump to search

Project Structure

/opt/pro/chorke/test
   ├─ bin/
   │   ├─ testd
   │   └─ testd.bat
   ├─ jks/
   │   ├─ server.jks
   │   └─ client.jks
   ├─ lib/gpl/
   │   ├─ commons-logging-1.1.1.jar
   │   ├─ httpclient-4.5.3.jar
   │   ├─ commons-io-2.4.jar
   │   └─ httpcore-4.4.6.jar
   ├─ src/
   │   └─ org/chorke/api/test/ChorkeServiceTest.java
   │
   └─ usr/bin/
       └─ .gitignore

Java Program

package org.chorke.api.test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ChorkeServiceTest {
    private static String WSDL_URL = "https://api.chorke.org:5443/test/service";
    
    public static void main(String[] args) throws HttpException, IOException {
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            URIBuilder uriBuilder = new URIBuilder(WSDL_URL);
            HttpGet post = new HttpGet(uriBuilder.addParameters(params).build());

            CloseableHttpClient client = HttpClients.createDefault();
            HttpResponse response = client.execute(post);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                String xml = IOUtils.toString(is, StandardCharsets.UTF_8);
                System.out.println(String.format("XML :\\n %s", xml));
            }else {
                String format = "Failed : HTTP error code : %s ";
                throw new RuntimeException(String.format(format, response.getStatusLine().getStatusCode()));
            }
            EntityUtils.consume(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Compile and Run

set "CKI_HOME=D:\opt\pro\chorke\test\project\dir"

set "JAVA_OPTS=-Djavax.net.ssl.trustStore=%CKI_HOME%\jks\client.jks"
set "JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStorePassword=storepasswd"
set "JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStore=%CKI_HOME%\jks\client.jks"
set "JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStorePassword=storepasswd"
set "JAVA_OPTS=%JAVA_OPTS% -Duser.home=%HOME%"
set "JAVA_OPTS=%JAVA_OPTS% -Duser.name=%USER%"

set "CKI_JCLASS_PATH=%CKI_HOME%\lib\gpl\*"
set "CKI_JCLASS_PATH=%CKI_JCLASS_PATH%;%CKI_HOME%\usr\bin"
set "CKI_JCLASS_MAIN=org.chorke.api.test.ChorkeServiceTest"

javac -cp %CKI_JCLASS_PATH% %CKI_HOME%\src\org\chorke\api\test\*.java -d %CKI_HOME%\usr\bin
java %JAVA_OPTS% -cp %CKI_JCLASS_PATH% %CKI_JCLASS_MAIN%