Reputation: 39
When passing an argument to my Java file I reach the server and get the desired response, when I Export it into a jar file and run it in terminal with java -jar jarfile.jar arg1 arg2 arg3
I get a response that indicates the server received the arguments it but the arguments, the same ones I used in the Java file, are incorrect.
Why would a jar file handle arguments different than a Java file?
My Java code
package com.fatbtc;
import com.fatbtc.util.HttpUtil;
import com.fatbtc.util.MD5Util;
import com.fatbtc.util.StringUtil;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
public class withdrawJar {
private static String url = "https://www.fatbtc.us";
private static String apiKey = "";
private static String apiSecret = "";
private static String signType = "MD5";
static String address;
static String currency;
static double amount;
public static void main(String[] args) {
withdraw(address, currency, amount);
}
public static void withdraw(String address, String currency, double amount) {
String reqUrl = String.valueOf(url) + "/order/api/withdraw";
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> map = new HashMap<>();
map.put("addr", address);
map.put("amount", Double.valueOf(amount));
map.put("site_id", Integer.valueOf(1));
map.put("api_key", apiKey);
map.put("currency", "ETH");
map.put("sign_type", signType);
map.put("timestamp", getSystemTimeStamp());
map.put("sign", MD5Util.createSign(map, apiSecret));
String params = mapper.writeValueAsString(map);
String response = HttpUtil.doPostJson(reqUrl, params);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Long getSystemTimeStamp() {
String reqUrl = String.valueOf(url) + "/m/timestamp";
try {
Long timestamp = StringUtil.getTimeStamp();
StringBuffer sBuffer = new StringBuffer(reqUrl);
sBuffer.append("/").append(timestamp);
String response = HttpUtil.doGet(sBuffer.toString());
ObjectMapper mapper = new ObjectMapper();
Map map = (Map)mapper.readValue(response, HashMap.class);
return Long.valueOf((String)map.get("data"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Upvotes: 0
Views: 55
Reputation: 703
I can't see the variable args
being used in your code.
Could you please explain how did you verify it was working when you ran this class alone?
public static void main(String[] args) {
withdraw(address, currency, amount);
}
You should use the arguments passed from the terminal. In the above part you are ignoring them. You can change them to something like as given below to get the result I guess.
public static void main(String[] args) {
String address = args[0];
String currency = args[1];
double amount = Double.valueOf(args[2]);
withdraw(address, currency, amount);
}
If you note, the variable declaration should also ideally be inside the main method if you want to make the reader of the code very clear what are the arguments and what you intend to do with them.
The scope of the variables address, currency, amount
is only inside the main method and hence you should not declare a static variable for that.
I would suggest you to read more on the scope of a variable in programming and also what are static variables and what do they mean for the JVM
Upvotes: 1