简体   繁体   中英

Error compiling a simple class that uses a single dependency jar file

I have a simple class (RJson.java) that imports org.json.JSONObject from json jar file.

import org.json.JSONObject;
public class RJson{
  public static void main(){
    JSONObject obj = new JSONObject();
  }}

and also I have downloaded the JAR file from here .

I can easily compile the RJson class using:

javac -cp json.jar RJson.java

and that creates RJson.class . But during runtime when I run:

java -cp json.jar RJson

I recieve the following error: Error: Could not find or load main class RJson .

Can someone please guide me how I can solve this problem? Note: I don't want to use Ant or Maven.

When you're running the code, your classpath only contains json.jar - not the current directory.

Run it as:

java -cp json.jar:. RJson

(or use a semi-colon instead of a colon if you're on Windows) and I suspect it'll be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM