Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By jashburn

Thread: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

  1. #1
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Exclamation Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    There is a specific function I have added to a program I've been working with for a while which involves retrieving data from a website. Here is that code:

    Java Code:
    public String getWebData(String urlString, String add) throws IOException{
            String output = "";
            try {
    //+s being the token, for example if dictionary.com was being used
                add = add.replace(" ", "+s");
                urlString = urlString + add;
                URL url = new URL(urlString);
                InputStream inputStream = url.openStream();
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer webPageData = new StringBuffer();
                String inputLine = null;
                while ((inputLine = inputReader.readLine()) != null) {
                    webPageData.append(inputLine);
                    webPageData.append("\n");
                }
                output = webPageData.toString();
                inputReader.close();
            } catch (MalformedURLException ex) {
                Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
            }
            return output;
        }
    Anyway, when I run this program within Netbeans, it works perfectly. I have a backup of the project in eclipse as well, and I've copied all of the code over and tried running the same thing in Eclipse - exactly the same, it works perfectly. The problem is whether I compile the the code in Netbeans or Eclipse, the exported runnable jar for some reason has an issue with this one method. It doesn't crash, and it seems to be doing something, but it is by no means giving me the data from the website like it is supposed to. Does anyone have any insight as to why this may be?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Are there error messages? Please copy them here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    No errors, just not doing what it does when run within Eclise/Netbeans

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Quote Originally Posted by ZhiMing View Post
    the exported runnable jar for some reason has an issue with this one method. It doesn't crash, and it seems to be doing something, but it is by no means giving me the data from the website like it is supposed to.
    Please elaborate on what you mean by "doing something". Also, what are the indications that there's an issue with this method?

    Visually your getWebData method seems fine. Therefore the problem may be elsewhere, perhaps in the caller of this method? You'd want to place println() or log statements at strategic points in your code to diagnose this further.

    Btw, is your class actually called "Functions"? I see you're using this class name to get your logger...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    not doing what it does when run within Eclise/Netbeans
    Can you show what it does outside the IDEs
    and what it does inside the IDEs
    so that the two results can be compared?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Java Code:
    public void BtnTraditionalAction(Layout layout) {
            try {
                String urlString = "http://chinesebay.com/chinesetools/cword2pinyin.php?text=";
                String add = layout.acsTbxOutput.getText();
                String output = funct.getWebData(urlString, add, "");
                output = output.replace("Pinyin w/numbers</td><td>", "");
                output = output.replace("</td></tr><tr><td>Pinyin w/Tone Marks", "");
                String outsplit[] = output.split("");
                output = outsplit[1];
                layout.acsTbxOutput.setText(output);
            } catch (IOException ex) {
                Logger.getLogger(Accessories.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    This is what calls the method. Yes, I have a class classed functions which is referenced as "funct." The stuff after the funct.getWebData part is just me parsing the data. When run within netbeans or eclipse, the data that comes out is html data from the website. When run outside as a runnable jar, the data that comes out is simply the input. I'm not sure if it would help to actually see the data that comes out when functioning and not functioning, but I can printscrn if that would help.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    the data that comes out
    Add a println() statement to print what comes out so it can be seen on the console.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Did you want to see the exact data? I can do so, but right now it's just printing it on the textbox in the JFrame form

    Java Code:
    layout.acsTbxOutput.setText(output);

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Add a println("output="+output +"<") statement just before that statement to show what is in output on the console.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    -----Output 1 (from within Netbeans/Eclipse)-----------------------------------------------------------------------------------------------------
    <html><head><title>Pinyin for:妳 at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />

    </head>
    <body>
    <center>
    <h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    ni3 </td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>nĭ </td></tr><tr><td>Traditional 繁(正)體</td><td>妳</td></tr><tr><td>Simplified 简体</td><td>你</td></tr></table>

    <hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great Help for Chinese Teachers!
    </body></html>
    -------------------------------------------------------------------------------------------------------------------------------------------------


    ------Output 2 (From runnable jar outside of compiler)------------------------------------------------------------------------------------------------------------------
    <html><head><title>Pinyin for嚗p at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />

    </head>
    <body>
    <center>
    <h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    </td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>妳</td></tr><tr><
    td>Traditional 蝜?甇??擃?/td><td>妳</td></tr><tr><td>Simplified 蝞??</td><td>
    妳</td></tr></table>

    <hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great He
    lp for Chinese Teachers!
    </body></html>[/HTML]
    -------------------------------------------------------------------------------------------------------------------------------------------------
    The top block of HTML is the output from when run within netbeans and/or eclipse. The bottom block is from when run independently. the relevant text in each block is underlined and in bold. In the top block, the inputted text is the character 妳, and the appropriate output is the result n13. However, for whatever reason in the second block, from the output of the program when used outside of netbeans/eclipse, the result is not, as it should be n13, but rather the original input, 妳. I'd be really grateful for some insight as to what's going on here, this makes no sense to me! Why would it work within the compiler but have this different result when run outside? I cannot emphasize enough, this is just one example, but I've tried this several times, each time with the exact same input for both the program when run outside of the compiler as well as inside. Exactly the same.

  11. #11
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    This may be a character encoding issue. I've noticed Output 1's title shows
    Java Code:
    <html><head><title>Pinyin for:妳 at ChineseBay.com</title>
    whereas Output 2 shows
    Java Code:
    <html><head><title>Pinyin for嚗p at ChineseBay.com</title>
    Output 2 also has question mark characters is a few other places.

    Looking at chinesebay.com's page info (right-click > "View Page Info" in Firefox on the returned page for 妳), the encoding of the page is in UTF-8. Therefore you'd also want your displays to use this encoding.

    Assuming the outputs above are from println (and therefore displayed on the command prompt console), and assuming you're using Windows, see if changing the code page as suggested at Unicode characters in Windows command line - how? - Stack Overflow helps.

    Also, while you're at it you'd want to change
    Java Code:
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
    to
    Java Code:
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    This may or may not help with your issue (I'm not sure because I'm on Linux and I've got UTF-8 everywhere...), but it's a best practice when dealing with multibyte characters, unless you know the specific encoding for the returned data, such as GB18030 or Big5, in which case you specify that exact encoding.

    Hth!

  12. #12
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    I changed the codepage to UTF-8 in command prompt as advised.

    I also changed the method as suggested:

    Java Code:
     public String getWebData(String urlString, String add, String divider) throws IOException {
            String output = "";
            try {
                add = add.replace(" ", "");
                add = add.replace("", divider);
                urlString = urlString + add;
                URL url = new URL(urlString);
                InputStream inputStream = url.openStream();
                //Relevant change below
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                //^^^^^^^^^^^^^^
                StringBuffer webPageData = new StringBuffer();
                String inputLine = null;
                while ((inputLine = inputReader.readLine()) != null) {
                    webPageData.append(inputLine);
                    webPageData.append("\n");
                }
                output = webPageData.toString();
                inputReader.close();
            } catch (MalformedURLException ex) {
                Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(output);
            return output;
        }
    With this change in code, the output when run within netbeans/eclipse remains exactly the same, but has changed (unfortunately not for the better) when run using the runnable jar.

    ------Output 2 (From runnable jar outside of compiler)------------------------------------------------------------------------------------------------------------------
    <html></head>
    <body>
    <center>
    <h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    ?A</td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>?A</td></tr><tr><
    td>Traditional c]^</td><td>?A</td></tr><tr><td>Simplified ?^</td><td>?A<
    /td></tr></table>

    <hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great He
    lp for Chinese Teachers!
    </body></html>[/HTML]
    -------------------------------------------------------------------------------------------------------------------------------------------------

    Not sure why change it to UTF-8 would do this! As I've said, I changed the code page in command prompt as advised by using this following command "chcp 65001," so this isn't command prompt that is displaying it wrong. Also, the output text in the swing textbook matches this output, and is simply "?A" (although the "?" becomes another symbol).

  13. #13
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    I tested this out on Windows today, and I think I've figured out what went wrong. It seems that there are 2 issues:
    1. UTF-8 output encoding
    2. Windows command prompt cannot display properly

    As far as I can tell,

    Java Code:
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    is correct. On top of that, you also need to output in UTF-8, which is the missing bit in your code. When you use "System.out.println()", afaik it uses the default encoding - whatever encoding that is already set when you ran the application via command line. However, even if you've done so, you will still not see the expected Chinese characters on the command prompt window even after using "chcp 65001" and setting the window to use the Lucida Console font. This seems to be an issue that Microsoft can't be bothered to fix...

    The following is my test code:

    Java Code:
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.*;
    
    public class Pinyin {
    	public String getWebData(String urlString, String add) throws IOException {
    		String output = "";
    		try {
    			// +s being the token, for example if dictionary.com was being used
    			add = add.replace(" ", "+s");
    			urlString = urlString + add;
    			URL url = new URL(urlString);
    			InputStream inputStream = url.openStream();
    			BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    
    			StringBuffer webPageData = new StringBuffer();
    			String inputLine = null;
    			while ((inputLine = inputReader.readLine()) != null) {
    				webPageData.append(inputLine);
    				webPageData.append("\n");
    			}
    			output = webPageData.toString();
    			inputReader.close();
    		}
    		catch (MalformedURLException ex) {
    			ex.printStackTrace();
    		}
    
    		return output;
    	}
    
    	public static void main(String[] args) throws IOException {
    		Pinyin pinyin = new Pinyin();
    		
    		String webData = pinyin.getWebData("http://chinesebay.com/chinesetools/cword2pinyin.php?text=%E4%BD%A0", "");
    		System.out.println("default:\n" + webData);
    		PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
    		sysout.println("\nutf8:\n" + webData);
    	}
    }
    I ran it via command line like this:

    Java Code:
    java Pinyin > output_printstream.txt
    I.e., I redirected the output to a text file rather than displaying it on the console. When I opened the text file using Notepad (I tried with Courier New and Lucida Console fonts), I could see that the output from System.out.println() is incorrect, whereas the output from sysout.println() (where the PrintStream object was explicitly set to use UTF-8) is correct.

    In short,
    • don't rely on the output on the Windows command prompt window to determine if Chinese characters have been encoded properly
    • always redirect the output to a text file, and view the text file using a UTF-8-capable application

    A possible reason (I'm speculating) the output can be displayed correctly in an IDE is that the IDE may be using UTF-8 throughout internally.
    gimbal2 likes this.

  14. #14
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,725
    Rep Power
    5

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    You are a winner, Jashburn. That is exactly right :)

    Generally an IDE allows you to configure what encoding is used internally. In Eclipse I manually have to set it to UTF-8 because it defaults to the platform/OS encoding (which is NOT UTF-8 on Windows as you already know).

    Ah the wonderful world of encoding hell. It becomes far less of a hell if you realize there are three points of failure; storage (ex in a file or in a database), loading (ex into memory/objects) and presentation (ex show in a webpage). Then all you have to do is figure out where exactly in those three places it goes wrong...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Sorry it took me so long to get around to testing this out! Unforunately, I changed the code as you said and what I got... was exactly the same as before pretty much, except there are subtle differences between the "Default" and the "UTF-8" although neither of them has the Chinese that is supposed to be outputted.

    Here's the code as I changed it:

    Java Code:
     public String getWebData(String urlString, String add, String divider) throws IOException {
            String output = "";
            try {
                add = add.replace(" ", "");
                add = add.replace("", divider);
                urlString = urlString + add;
                URL url = new URL(urlString);
                InputStream inputStream = url.openStream();
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                StringBuffer webPageData = new StringBuffer();
                String inputLine = null;
                while ((inputLine = inputReader.readLine()) != null) {
                    webPageData.append(inputLine);
                    webPageData.append("\n");
                }
                output = webPageData.toString();
                inputReader.close();
            } catch (MalformedURLException ex) {
                Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("default:\n" + output);
            PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
            sysout.println("\nutf8:\n" + output);
            return output;
        }
    And here's my output in Console:

    XML Code:
    Active code page: 65001
    java -jar LMCC18.jar
    default:
    <html><head><title>Pinyin forG?? at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />
    
    </head>
    <body>
            <center>
            <h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    ??</td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>??</td></tr><tr><
    td>Traditional c]^</td><td>??</td></tr><tr><td>Simplified ?^</td><td>??<
    /td></tr></table>
    
            <hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great He
    lp for Chinese Teachers!
    </body></html>
    
    
    utf8:
    <html><head><title>Pinyin for:�� at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />
    
    </head>
    <body>
            <center>
            <h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    ��</td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>��</td></
    tr><tr><td>Traditional 繁(*)體</td><td>��</td></tr><tr><td>Simplifie
    d 简体</td><td>��</td></tr></table>
    
            <hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great He
    lp for Chinese Teachers!
    </body></html>
    Meanwhile, if instead of using console I just have that same string output to a text area, it comes out like this (when run outside of Netbeans/Eclipse, of course):

    XML Code:
    <html><head><title>Pinyin for:�� at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />
    
    </head>
    <body>
    	<center>
    	<h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    ��</td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>��</td></tr><tr><td>Traditional 繁(正)體</td><td>��</td></tr><tr><td>Simplified 简体</td><td>��</td></tr></table>	
    	
    	<hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great Help for Chinese Teachers!
    </body></html>
    Finally, here is what it is supposed to look like (and what it does in fact look like when run in Netbeans and outputted in a textbox or in the Netbeans console):

    XML Code:
    <html><head><title>Pinyin for:我 at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />
    
    </head>
    <body>
    	<center>
    	<h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    wo3 </td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>wŏ </td></tr><tr><td>Traditional 繁(正)體</td><td>我</td></tr><tr><td>Simplified 简体</td><td>我</td></tr></table>	
    	
    	<hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great Help for Chinese Teachers!
    </body></html>
    This is killing me guys But thanks for the effort. Still can't figure out why the Chinese keeps getting changed into This -> �. The codepage has been changed in console, and i've done the UTF stuff for both the input stream reader as well as the system output command.
    Last edited by ZhiMing; 03-20-2014 at 12:53 PM.

  16. #16
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    I previously wrote,

    Quote Originally Posted by jashburn View Post
    I.e., I redirected the output to a text file rather than displaying it on the console. When I opened the text file using Notepad (I tried with Courier New and Lucida Console fonts), I could see that the output from System.out.println() is incorrect, whereas the output from sysout.println() (where the PrintStream object was explicitly set to use UTF-8) is correct.
    Did you redirect the output into a text file to be viewed using Notepad using 1 of the fonts mentioned above?

    If you did and the results are still not right, zip up your source and class files, and attach them here. It may be an issue with your environment.

  17. #17
    ZhiMing is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    Very interesting! When displayed in Courier New, the output is different, however still not what I should be getting.

    Java Code:
    <html><head><title>Pinyin for嚗蕭嚙?at ChineseBay.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="table.css" />
    
    </head>
    <body>
    	<center>
    	<h1>Convert Chinese Words To Pinyin</h1>
    <form action="" method=get>
    <textarea name=text cols=60 rows=3></textarea>
    <br/><input type=submit value="Convert!"></form>
    <table><tr><td>Pinyin w/numbers</td><td>
    嚙踝蕭</td></tr><tr><td>Pinyin w/Tone Marks</td><td class='pinyin'>嚙踝蕭</td></tr><tr><td>Traditional 蝜?甇??擃?/td><td>嚙踝蕭</td></tr><tr><td>Simplified 蝞雿?/td><td>嚙踝蕭</td></tr></table>	
    	
    	<hr><a href='/chinesetools'>Chinese Tools at ChineseBay.com</a> Great Help for Chinese Teachers!
    </body></html>
    In any event, the entire project is very big, so I've recreated it in a new project so that only the code relevant to this specific feature is included. I've exported the netbeans project and attached it.
    Attached Files Attached Files

  18. #18
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    We still have some encoding grief. I can confirm that running the application directly in NetBeans on Windows inputting 妳 will return ni3, but on command line (started using java Layout) it will return some garbage characters.

    Something that's noteworthy is the source files cannot be compiled (again, on Windows) via command line:
    Java Code:
    C:\tmp\WebDataTest\WebDataTest\src>javac *.java
    Webdata.java:28: error: unmappable character for encoding GBK
                output = output.replace("鈥?, " ");
                                          ^
    Webdata.java:38: error: unmappable character for encoding GBK
                //For example, the word 濡?should come out as "ni3", however when run outside of netbeans I always get "锟絧"
                                         ^
    Webdata.java:28: error: unclosed string literal
                output = output.replace("鈥?, " ");
                                               ^
    3 errors
    However, it compiles without issues if I use javac -encoding UTF-8 *.java. In other words, the source files themselves were saved encoded using UTF-8.

    As mentioned above, the output displays garbage characters if the application was started using java Layout, however it displays "ni3" correctly if it was started using java -Dfile.encoding=UTF-8 Layout!

    I'd like to understand this a bit better... maybe by remotely attaching a debugger to it to step through the code or something. Will have to do this later...

  19. #19
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Works in Netbeans, works in Eclipse, doesn't work after being compiled by either

    I think I've finally figured out the last piece to get this working consistently. It remains that you should continue using
    Java Code:
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    Apart from that, if you want to print out the web site's response to be viewed externally (i.e., not in a JTextArea), you should still use
    Java Code:
    PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
    and redirect the output to a file to be viewed using Notepad because the Windows command prompt window doesn't support UTF-8 properly.

    The final piece is in URL-encoding the character to be sent to the web site. E.g., for the code you attached in your last post I changed
    Java Code:
    urlString = urlString + add;
    to
    Java Code:
    urlString = urlString + URLEncoder.encode(add, "UTF-8");
    This works for me on (English) Windows even when I used code page 1252, and ran Layout via command line without specifying the -Dfile.encoding=UTF-8 startup parameter that I mentioned in my last reply.

    URL encoding is required in the query string - the part after the ? character in the URL http://chinesebay.com/chinesetools/cword2pinyin.php?text=chinese_char - for the name=value pair. The name part in this query string is 'text'. URL-encoding 'text' will still be 'text', so there's no need to encode it in this particular case. For the value part, chinese_char, e.g., 妳, needs to be encoded so that it'll be sent to the server as %E5%A6%B3.

    As before, if the problem persists, zip up your source and class files, and attach them here.

Similar Threads

  1. Replies: 3
    Last Post: 03-03-2014, 11:19 AM
  2. chat applications works and doesn't
    By willemjav in forum Networking
    Replies: 9
    Last Post: 09-16-2013, 12:41 PM
  3. Replies: 5
    Last Post: 06-02-2011, 06:59 AM
  4. Replies: 3
    Last Post: 04-15-2010, 02:41 AM
  5. one works, mine doesn't
    By psychop in forum Java Applets
    Replies: 0
    Last Post: 01-25-2010, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •