Search code examples
javac++apitwittertwitter4j

Unable to load main class when compiling and running a java file using C++


I've been trying to compile and execute a java file from C++ but I am currently getting this error. The java file works fine when executed separately in java.

This is the error code received:

Error: Could not find or load main class TwitterDataCrawler
Caused by: java.lang.ClassNotFoundException: TwitterDataCrawler

In my C++ file:

(TwitterDataCrawler.java is the file i wish to run \ twitter4j-core-4.0.7.jar is the twitter API that is required to run)

int main()
{
    system("javac -cp twitter4j-core-4.0.7.jar TwitterDataCrawler.java");
    system("java -cp TwitterDataCrawler.java TwitterDataCrawler arg1");
    return 0;
}

The structure of my java file looks something like this:

public class TwitterDataCrawler {
    public TwitterDataCrawler() {
    }   
    public static void viewAccident(int x) throws TwitterException, IOException{
    }
    public static void main(String[] args) throws TwitterException, IOException {
            viewAccident(5000);
    }
}

I have to call the public static void main function from within the class TwitterDataCrawler. I assume that since the class does contains a main function, targeting the class with an argument should work. Could someone point me in the right direction?


Solution

  • Solved!

    system(javac -cp twitter4j-core-4.0.7.jar TwitterDataCrawler.java);
    system(java -cp .;twitter4j-core-4.0.7.jar TwitterDataCrawler);
    

    Since the class created from using javac is dependent on a jar API(both residing in the same directory), ".;twitter4j-core-4.0.7.jar" is included in order to execute TwitterDataCrawler.