Building a Jar file in Intellij 在Intellij中构建一个Jar文件

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

Building a Jar file in Intellij

问题

我是新手使用Java,并且正在使用Intellij。我有一个src文件夹,我在其中编写了我的Java程序。我必须构建我的Jar文件,但我目前遇到了困难。

我已经构建了Jar文件,并尝试在命令行中运行以下命令:

java -jar Project.jar input.json output.json

但是在运行程序时,我遇到了以下错误:

Exception in thread "main" net.sf.json.JSONException: JSONObject["employee_type"] is not a number.

我不确定为什么会出现这个错误。当我通过配置参数运行我的代码并进行调试时,程序可以正常运行,但构建后却出现问题。

我有这些库,但我不确定它们是否都有用:

commons-collections-3.2.1.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar

这是我的JSON文件:

{
 "id_employee":123456789,
 "employee_type":2,
 "min_pay":"35.47 $",
 "max_pay":"72.03 $"
}

这是读取JSON文件的一部分代码,错误发生在第四行:

String myJSON = loadFileIntoString(args[1], "UTF-8");
JSONObject jsonEmployee = JSONObject.fromObject(myJSON);
int employeeID = jsonEmploye.getInt("id_employee");
int employeeType = jsonEmploye.getInt("employee_type");
String minHourlyRateString = jsonEmploye.getString("min_pay");
String maxHourlyRateString = jsonEmploye.getString("max_pay");

我已经在这里挣扎了一段时间,不完全明白构建是否有问题或其他什么原因。我尝试按照教程正确构建程序成为jar文件,但仍然出现无法正确读取我的JSON文件的错误。我的代码似乎没有问题,因为运行和调试都如预期那样工作,所以任何建议都将非常有帮助。

英文:

I am new to Java and I am using Intellij. I have a src folder where I wrote my Java program. I have to build my Jar file but I am struggling so far.

I have built the Jar file and I try running this in the command line :

java -jar Project.jar input.json output.json

But I have this error when running the program :

Exception in thread "main" net.sf.json.JSONException: JSONObject["employee_type"] is not a number.

I am not sure to understand why it does that. When I run my code by configuring the arguments and when debugging, the program works without any problem, but not the build.

I have these libraries but I am not sure if all of them are useful:

commons-collections-3.2.1.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar

Here is my JSON file :

{
 "id_employee":123456789,
 "employee_type":2,
 "min_pay":"35.47 $",
 "max_pay":"72.03 $"
}

And here bit of the code that reads the JSON file, the error happens at the 4th line :

String myJSON = loadFileIntoString(args[1], "UTF-8");
JSONObject jsonEmployee = JSONObject.fromObject(myJSON);
int employeeID = jsonEmploye.getInt("id_employee");
int employeeType = jsonEmploye.getInt("employee_type");
String minHourlyRateString = jsonEmploye.getString("min_pay");
String maxHourlyRateString = jsonEmploye.getString("max_pay");

I have stuggled here for a while, not fully understanding if the build is wrong or anything else. I tried to follow the tutorials to build correctly the program into a jar file, but it still gives me the error that it can't read correctly my JSON file. My code doesn't seems to be an issue since the run and debug works as expected, so any advice would be great.

答案1

得分: 1

欢迎加入开发社区。

我不了解所有的细节,但根据我看到的代码,我认为问题出在这一行:"String myJSON = loadFileIntoString(args[1], "UTF-8");"

事实上,你的命令行是:java -jar Project.jar input.json output.json
但args[1]不是指向input.json,而是output.json。如果你想要读取input.json文件,应该使用args[0]。

英文:

Welcome in the dev community.

I don't have all the details but when I see your code, in my opinion the issue is on the line "String myJSON = loadFileIntoString(args[1], "UTF-8");"

Indeed your command line : java -jar Project.jar input.json output.json
but the args[1] is not the input.json but the output.json, if you want to read the input.json file you should use args[0].

huangapple
  • 本文由 发表于 2023年6月6日 10:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411043.html
匿名

发表评论

匿名网友

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

确定