0

I am trying to use the JSON simple library, but I just can'e execute my program with it.

I am trying to compile this way:

$ javac -classpath json.jar TestClass.java (json.rar is the json-simple file)

And it compiles ok. But I'm having trouble running it. This is how I am trying to run the program:

$ java -classpath json.jar:. TestClass

And I get the following error: Error: Could not find or load main class TestClass

But the file IS there. After an ls, this show up:

DBImporter.class json-simple-1.1.1.jar TestClass.class testfile2.txt DBImporter.java output.txt TestClass.java json.jar resource.db testfile.txt

I'm searching on the internet for hours, and no solution works for me. I don't know if this is relevant, but this is my TestClass

import java.io.*;
import org.json.simple.*;

public class TestClass{

    public static void main(String[] args){

        JSONObject obj = new JSONObject();

    }
}

Thanks in advance!

Miguel Péres
  • 630
  • 1
  • 10
  • 19

2 Answers2

0

Try this

java -classpath json.jar;. TestClass

Class:

import java.io.*;
import org.json.*;

    public class TestClass{

        public static void main(String[] args){

            JSONObject obj = new JSONObject();
     System.out.println("Hello");
        }
    }

Directory Structure : G:\Mudit\Test - inside this test directory I have put json.jar and TestClass.java and I am running the following commands:

Compile :javac -classpath json.jar TestClass.java Run: java -classpath json.jar;. TestClass

Output: Hello Hope this clarifies.

Mudit Shukla
  • 814
  • 2
  • 6
  • 16
  • When trying this I receive: `Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include: [with a bunch of options here]` and in the very last line `bash: TestClass: No such file or directory` But since I'm using mysys shell (which accept linux commands) I tried `$ java -classpath json.jar:. TestClass` instead, but the response was `Error: Could not find or load main class TestClass`. – Miguel Péres May 13 '15 at 15:35
  • No. Here, this is all that shows up when I tried the commands: http://i.imgur.com/uRjiFrz.png – Miguel Péres May 13 '15 at 15:52
  • $ java -classpath json.jar;. TestClass . instead of using :(colon) use ;(semicolon), let me know if does not work. – Mudit Shukla May 13 '15 at 15:54
  • Unfortunately I've tried both already. You can see in the screenshot, in the second command I use the semicolon, and in the third one, the colon. :( – Miguel Péres May 13 '15 at 15:58
  • I am not able to see the screenshot. – Mudit Shukla May 13 '15 at 16:03
  • 1
    Oh, sorry. It is in this link ( i.imgur.com/uRjiFrz.png ). Anyway, when I try with semicolon it doensn't recognize the command and says `bash: TestClass: No such file or directory`. When I try with colon, it says `Error: Could not find or load main class TestClass`. – Miguel Péres May 13 '15 at 16:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77735/discussion-between-mudit-and-fmp). – Mudit Shukla May 13 '15 at 16:09
0

I managed to run the program using the following command:

$ java -cp json.jar:./ TestClass

Putting a slash after the dot. I'm not sure why it worked, if anyone know the explanation it would be nice, mainly because I haven't seen this solution anywhere when searching for it.

Miguel Péres
  • 630
  • 1
  • 10
  • 19