So my code is working fine when running from Eclipse, but executing the project's runnable jar does not.
Here is my code:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class cc {
public static HttpURLConnection con;
public static String url = "http://bac.onec.dz/index.php";
public static int begin;
public static int max;
public static boolean paused=false;
public static String path="result.csv";
public static void setData(int b,int m){
begin=b;max=m;
}
public static void main(String[] args) throws Exception {
int i =1;
setData(35043487,2);
while (i<=max && !paused) {
sendPost(begin++);
i++;
}
}
private static void sendPost(int mat) throws Exception {
String urlParameters ;
URL obj = new URL(url);
BufferedReader in;
StringBuffer response;
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Host", "bac.onec.dz");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Referer", "http://bac.onec.dz/");
con.setRequestProperty("Cookie", "bbbbbbbbbbbbbbb=ANNCLHAEAAAAAAAAJHKBHDAAAAAAAAAAEADAOFFHFHFHAAAADAAANKIFFKIFAAAA; TS1ff960=20859ee226968392c837af0430f21cf0087e23d48701410f5785a62879b49ee6533a75145e5dca68b041d236; aaaaaaaaaaaaaaa=GAABBCCHICBGBAAECAAAAAKCHHAAAAAAEACAALAAKGFHAAAADAAAAGAACKIFAAAA");
con.setDoOutput(true);
urlParameters = "matriculebac="+mat+"&dobac=%D8%A7%D8%B3%D8%AA%D8%B8%D9%87%D8%A7%D8%B1+%D8%A7%D9%84%D9%86%D8%AA%D9%8A%D8%AC%D8%A9";
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
in = new BufferedReader( new InputStreamReader(con.getInputStream()));
response = new StringBuffer();
Object[] aa = in.lines().toArray();
for (Object object : aa) response.append(object.toString()+"\n");
in.close();
Pattern pattern = Pattern.compile("alert\\(.*?\\);");
Matcher matcher = pattern.matcher(response.toString());
String[] person = null;
if (matcher.find())
{
String p =matcher.group(0).substring(7, matcher.group(0).length()-3);
person = p.split("\\\\n");
}
try {
String [] pp=
{
person[0 ],
person[1 ].split(":")[1].substring(1),
person[2 ].split(":")[1].substring(1),
person[10].split(":")[1].substring(1),
person[4 ].split(":")[1].substring(1),
person[5 ].split(":")[1].substring(1),
person[6 ].split(":")[1].substring(1),
person[7 ].split(":")[1].substring(1),
};
CvsWrite(pp);
} catch (Exception e) {
}
}
public static void CvsWrite(String[] args) throws IOException {
List<String> pers = new ArrayList<>();
for (String string : args) pers.add(string);
String clct1 = pers.stream().collect(Collectors.joining(",")) + "\r\n";
File f = new File(path);
if (!f.exists())
try {
f.createNewFile();
List<String> test = new ArrayList<>();
test.add("الملاحظة");
test.add("رقم التسجيل");
test.add("الشعبة");
test.add("المعدل");
test.add("اللقب");
test.add("الاسم");
test.add("مكان الميلاد");
test.add("تاريخ الميلاد");
String clct2 = test.stream().collect(Collectors.joining(","))+ "\r\n\r\n";
Files.write(Paths.get(path),clct2.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
Files.write(Paths.get(path),clct1.getBytes(), StandardOpenOption.APPEND);
}
}
Running from eclipse, the output file is perfect with UTF-8 encoding.
Running from exported jar, a part of the output file is in ANSI encoding. Now that wouldn't be a problem if I'm writing just these words, but the code writes other content in Arabic, and the output be like
Output file was opened with Notepad++ in ANSI http://i.stack.imgur.com/6RGEj.png in UTF-8 http://i.stack.imgur.com/Li0qf.png
As I mentioned this problem hasn't encountered when running from eclipse
Well that just sounds like you're getting different default encodings when running in different ways - which is somewhat to be expected.
Just specify the encoding when you convert the text to binary:
byte[] bytes = col.getBytes(StandardCharsets.UTF_8);
Files.write(Paths.get(path), bytes, StandardOpenOption.Append);
(I would have suggested using Files.write
and passing in the text directly, but that will use the platform-default line break, which would be a separate issue...)
You've got the same problem when creating an InputStreamReader
:
in = new BufferedReader( new InputStreamReader(con.getInputStream()));
That's assuming that the data is in the platform-default encoding. Never assume that. You should find out what the actual encoding of the data is, and use that to read. Then decide (as a separate matter) what encoding you want to write in.
See more on this question at Stackoverflow