簡體   English   中英

如何運行包含.jar文件的java程序

[英]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。

  1. 主要問題是,如何使用.jar文件運行CLASS1和CLASS2?
  2. 為什么基於LINUX的系統與WINDOWS NETBEANS有如此不同? 我的意思是java是獨立於平台的......
  3. 我可以通過SFTP將NETBEANS連接到我的服務器,所以我可以直接在那里編碼。

謝謝。

正在編輯

我嘗試了以下命令,它們都不起作用,它給我的消息就像我正在運行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM