[英]How to run a java program including a .jar file
我有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
任何人都可以幫助我(聊天會更好)? 我真的需要這個
有了這個:
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
請注意,您的類路徑在編譯和執行時是相同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.