为什么一个Java文件和一个Jar文件的返回结果会不同?

huangapple go评论90阅读模式
英文:

Why would a Java file and Jar file have different returns?

问题

以下是您提供的Java代码的翻译部分:

当我向我的Java文件传递参数时我可以到达服务器并获得所需的响应但当我将其导出为jar文件并在终端中使用`java -jar jarfile.jar arg1 arg2 arg3`运行时我得到一个响应表明服务器接收到了参数但是与Java文件中使用的相同参数不正确

为什么jar文件处理参数的方式与Java文件不同

我的Java代码

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;
    } 
  }

}

请注意,翻译中仅包含代码部分,没有额外的内容。

英文:

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 = &quot;https://www.fatbtc.us&quot;;
private static String apiKey = &quot;&quot;;
private static String apiSecret = &quot;&quot;;
private static String signType = &quot;MD5&quot;;
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) + &quot;/order/api/withdraw&quot;;
ObjectMapper mapper = new ObjectMapper();
try {
Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;addr&quot;, address);
map.put(&quot;amount&quot;, Double.valueOf(amount));
map.put(&quot;site_id&quot;, Integer.valueOf(1));
map.put(&quot;api_key&quot;, apiKey);
map.put(&quot;currency&quot;, &quot;ETH&quot;);
map.put(&quot;sign_type&quot;, signType);
map.put(&quot;timestamp&quot;, getSystemTimeStamp());
map.put(&quot;sign&quot;, 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) + &quot;/m/timestamp&quot;;
try {
Long timestamp = StringUtil.getTimeStamp();
StringBuffer sBuffer = new StringBuffer(reqUrl);
sBuffer.append(&quot;/&quot;).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(&quot;data&quot;));
} catch (Exception e) {
e.printStackTrace();
return null;
} 
}
}

答案1

得分: 1

我在你的代码中没有看到变量 args 被使用。
你能否解释一下,在你单独运行这个类时是如何验证它在工作?

public static void main(String[] args) {
    withdraw(address, currency, amount);
}

你应该使用从终端传递的参数。在上面的部分,你忽略了它们。
你可以将它们更改为像下面给出的方式,以获得我猜想的结果。

public static void main(String[] args) {
    String address = args[0];
    String currency = args[1];
    double amount = Double.valueOf(args[2]);
    withdraw(address, currency, amount);
}

如果你注意到的话,变量声明在主方法内部也理应如此,这样你就可以让代码的读者非常清楚地知道参数是什么,以及你打算如何使用它们。
变量 address, currency, amount 的作用域仅限于主方法内部,因此不应该为它们声明静态变量。
我建议你更多地了解编程中变量的作用域,以及静态变量的含义以及对 JVM 的影响。

英文:

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);
}

<!-- end snippet -->
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

huangapple
  • 本文由 发表于 2020年4月6日 00:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61045967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定