[英]How to run a java program including a .jar file
I have 2 classes 我有2节课
CLASS1 (the main class)
- calls threading glass
CLASS2 (the threading class)
- calls function
- calls json.simple.jar functons
and a .JAR ( https://code.google.com/p/json-simple/ ) 和.JAR( https://code.google.com/p/json-simple/ )
jar/json.simple.jar
Now the main problem is that i actualy managed to make both classes to compile with those commands 现在主要问题是我实际上设法使用这些命令编译两个类
javac -d . -cp '.:jar/json.simple.jar' *.java;
this will create a folder named package1 and then when i try to run it it gives me the error 这将创建一个名为package1的文件夹,然后当我尝试运行它时,它会给我错误
java javanolo.CLASS1 -jar 'jar/json.simple.jar';
Selected (9) IPS ... <-- this is the first **println**
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at javanolo.CLASS1.main(CLASS1.java:70)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
and i assume this is because it is not finding some of the json simple functions. 我认为这是因为它没有找到一些json简单函数。 Why that?
为什么? I've imported them
我已经进口了它们
// json encode/decode
import org.json.simple.JSONValue;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
and i am sure that i have imported the .jar, because if i run 而且我确信我已经导入了.jar,因为如果我跑了
javac -d . *.java;
error :
import org.json.simple.JSONValue;
^
CLASS1.java:14: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
^
CLASS1.java:15: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
^
CLASS2.java:16: error: package org.json.simple does not exist
import org.json.simple.JSONValue;
^
CLASS2.java:17: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
^
CLASS2.java:18: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
^
CLASS2.java:148: error: cannot find symbol
public Map<String,String> openAndGetGeoDataByProxy(String ip,String port,int timeout) throws IOException, InterruptedException, ParseException
^
symbol: class ParseException
location: class CLASS2
CLASS2.java:99: error: cannot find symbol
} catch (ParseException ex) {
^
symbol: class ParseException
location: class CLASS2
CLASS2.java:188: error: cannot find symbol
Object jsonObject = JSONValue.parse(line);
^
symbol: variable JSONValue
location: class CLASS2
CLASS2.java:191: error: cannot find symbol
JSONObject jsonArray = (JSONObject)jsonObject;
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:191: error: cannot find symbol
JSONObject jsonArray = (JSONObject)jsonObject;
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:194: error: cannot find symbol
JSONObject jsonArray2 = (JSONObject)jsonArray.get("array_result");
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:194: error: cannot find symbol
JSONObject jsonArray2 = (JSONObject)jsonArray.get("array_result");
^
symbol: class JSONObject
location: class CLASS2
so i know for sure that json-simple.jar is being used when compilation. 所以我肯定知道编译时会使用json-simple.jar。
Thanks. 谢谢。
EDIT TRIES 正在编辑
i have tried the following commands, none of them works, it gives me the message like i was running the java -help
command 我尝试了以下命令,它们都不起作用,它给我的消息就像我正在运行
java -help
命令
try1 java -cp .;jar/json.simple.jar javanolo.CLASS1
try1
java -cp .;jar/json.simple.jar javanolo.CLASS1
try2 java -cp .;'/root/folder/folder/jar/json.simple.jar' javanolo.CLASS1
try2
java -cp .;'/root/folder/folder/jar/json.simple.jar' javanolo.CLASS1
try3 java -cp .;/root/folder/folder/jar/json.simple.jar javanolo.CLASS1
try3
java -cp .;/root/folder/folder/jar/json.simple.jar javanolo.CLASS1
can anyone help me please (would be better on chat)? 任何人都可以帮助我(聊天会更好)? i really need this
我真的需要这个
With this: 有了这个:
java javanolo.CLASS1 -jar 'jar/json.simple.jar'
you run your class and give the rest of the command line, ie -jar jar/json.simple.jar
as arguments. 你运行你的类并给出命令行的其余部分,即
-jar jar/json.simple.jar
作为参数。
What you want is: 你想要的是:
java -cp .;jar/json.simple.jar javanolo.CLASS1
or, in a UNIX shell: 或者,在UNIX shell中:
java -cp .:jar/json.simple.jar javanolo.CLASS1
Note that your class path is the same in compilation and execution. 请注意,您的类路径在编译和执行时是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.