我有2个班级
CLASS1 (the main class)
- calls threading glass
CLASS2 (the threading class)
- calls function
- calls json.simple.jar functons
和.JAR(https://code.google.com/p/json-simple/)
jar/json.simple.jar
现在主要问题是我实际设法使用这些命令编译两个类
javac -d . -cp '.:jar/json.simple.jar' *.java;
这将创建一个名为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
我认为这是因为它没有找到一些json简单函数。为什么?我已经导入了它们
// json encode/decode
import org.json.simple.JSONValue;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
并且我确信我已导入.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
所以我确定在编译时会使用json-simple.jar。
感谢。
编辑搜索
我已经尝试了以下命令,它们都不起作用,它给我的消息就像我正在运行java -help
命令
try1
java -cp .;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
任何人都可以帮助我(聊天会更好)吗?我真的需要这个
答案 0 :(得分:2)
有了这个:
java javanolo.CLASS1 -jar 'jar/json.simple.jar'
运行您的类并提供命令行的其余部分,即-jar jar/json.simple.jar
作为参数。
你想要的是:
java -cp .;jar/json.simple.jar javanolo.CLASS1
或者,在UNIX shell中:
java -cp .:jar/json.simple.jar javanolo.CLASS1
请注意,您的类路径在编译和执行时是相同的。