0

I have written a java method which calls an API of Japanese dictionary->Parse the JSON response->Return first node(String) to caller as appropriate translation of English word. Program works perfectly fine when executed from Eclipse IDE. However, when I run it's executable jar ( created using export feature of Eclipse) I get no response from API. I am failing to understand cause of this behavior and would really help if anyone guide me on this.

I always get "GET request not worked" printed on console when I execute entire program as

java -jar JtoETranslate.jar

Java Method:

public static String sendGET(String param) throws IOException
     {
            BufferedWriter writer = null;
        Properties systemSettings =  System.getProperties();
            systemSettings.put("proxySet", "true");
             systemSettings.put("http.proxyHost","172.29.44.61");
             systemSettings.put("http.proxyPort", "8080");
            URL obj = new URL(GET_URL+param);
            HttpURLConnection con = (HttpURLConnection)obj.openConnection();
            con.setRequestMethod("GET");

            int responseCode = con.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK)
           {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();

        // print result

        try
        {
            writer = new BufferedWriter( new FileWriter("./docs/response.json"));
            writer.write(response.toString());

        }
        catch ( IOException e)
        {
        }
        finally
        {
            try
            {
                if ( writer != null)
                writer.close( );
               return parseJSON("./docs/response.json");
            }
            catch ( IOException e)
            {
            }
        }
        System.out.println("Got response");
        } else {
        System.out.println("GET request not worked");
    }return null;
        }

EDIT

As suggested in one of comments below, I added a debugging statement to see exactly what request is getting formed, and here is the request to API.

http://jisho.org/api/v1/search/words?keyword=????

Can anyone help me for how can I make Jar understand for UTF-8 encoding.

MKay
  • 818
  • 9
  • 32
  • Did you try to run the jar in admin modus? – Casper Jun 27 '17 at 09:38
  • If you are going to solve this problem by yourself, I'd suggest you to try remote debug by following [this](https://stackoverflow.com/questions/1732259/eclipse-how-to-debug-a-java-program-as-a-jar-file). Or could you please post all the code you wrote? – kenshinji Jun 27 '17 at 09:42
  • @Casper Yes I did. But same problem persists. – MKay Jun 27 '17 at 09:42
  • It could be you need to create a manifest and define the priviliges your program needs. such as internet connectivity. look at this url : http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/manifest.html – Casper Jun 27 '17 at 09:45
  • You should add some debugging code. How is the parameter set on the command line? What actual HTTP response code is returned by the server? Is an exception thrown?... – DrHopfen Jun 27 '17 at 09:46
  • @Casper : Thanks for sharing that URL. That can be possible issue. Can you help me with an example of manifest file including proxy setting. – MKay Jun 27 '17 at 10:04
  • @MKay If I'm honest I never used it since I don't need it for my work. I only know about its existence. But I know there are enough examples on the internet. Good luck! – Casper Jun 27 '17 at 10:37
  • Some correct code indentation would have helped here. You get the output `GET request not worked` if `responseCode` is anything else than `HttpURLConnection.HTTP_OK` so it might be a good idea to check what `responseCode` actually is (by printing it out?) and it may give you a hint of what's going wrong. – DanielBarbarian Jun 27 '17 at 10:56
  • @DanielBarbarian : Yes , when I try to output a request onto console , I get request as **http://jisho.org/api/v1/search/words?keyword=????** Because that ? is actually a japanese character , it is not somehow encoded with UTF-8. Can you help me to understand how I can make it work. – MKay Jun 28 '17 at 06:54
  • Yeah, that can happen but it does not mean your request is incorrectly encoded, sometimes you can get issues with the character encoding in the log only. But here I was asking about the `responseCode` and not the request. – DanielBarbarian Jun 28 '17 at 08:04
  • @DanielBarbarian : Well response code 500 as the request I sent had some garbage characters in parameters. I could able to solve the main issue by invoking jar with -Dfile.encoding=utf-8 – MKay Jun 28 '17 at 08:48
  • Ok, I am starting to get it now. Can you send the parameter with a post instead of a get? Might be easier to control the character encoding. – DanielBarbarian Jun 28 '17 at 09:29

0 Answers0