0

On a CentOS 7 server, I have a Java program /path/to/parent/bin/mainpackage/SendText.class that works perfectly when I call it from a c program located at /path/to/parent/bin using the syntax:

system("java -cp .:\"/path/to/parent/dependencies/*\" mainpackage.SendText username pincode");  

But the problem is that the C program cannot be located in /path/to/parent/bin. Instead, the C program must be located in /path/to/parent/. How can I change the syntax so that the Java program will run when called from /path/to/parent/?

Here is what I have done to set things up so far:

The Java program was compiled from /path/to/anotherparent/ using the command

javac -d bin -cp .:/path/to/anotherparent/dependencies/twilio-java-sdk-3.4.5.jar:/path/to/anotherparent/dependencies/httpcore-4.1.2.jar SendText.java

I then created the destination and copied SendText.class and its dependencies to the destination as follows:

[user@domain somedir]$ mkdir /path/to/parent/bin/
[user@domain somedir]$ mkdir /path/to/parent/bin/mainpackage
[user@domain somedir]$ sudo cp /home/username/javacode/bin/mainpackage/SendText.class /path/to/parent/bin/mainpackage
[user@domain somedir]$ sudo mkdir /path/to/parent/dependencies
[user@domain somedir]$ sudo cp -R /home/username/javacode/dependencies/* /path/to/parent/dependencies

Next, I created the C program atest.c in /path/to/parent/ as follows:

[user@domain somedir]$ cd /path/to/parent
[user@domain parent]$ sudo nano atest.c

int main (void){
    char jcmd[256] = "java -cp .:\"/path/to/parent/dependencies/*\" mainpackage.SendText username pincode";
    printf(jcmd);
    system(jcmd);
    return 0;
}

Ctrl-X to save the program  

I compiled the C program and tried to run it from /path/to/parent/ as follows:

[user@domain parent]$ sudo cc atest.c -o atest
[user@domain parent]$ ./atest
Error: Could not find or load main class mainpackage.SendText
java -cp .:"/path/to/parent/dependencies/*" mainpackage.SendText username pincode

To confirm that the problem is the location of the C file, I did the following:

[user@domain parent]$ sudo cp atest bin
[user@domain parent]$ cd /path/to/parent/bin
[user@domain bin]$ ./atest

Program runs successfully from /path/to/parent/bin

So what do I change in order to get the Java program to run successfully when it is called from /path/to/parent/ with the command ./atest?

0

1 Answer 1

1
javac -d bin ...

That means that bin, as an absolute path, must appear in the CLASSPATH when you run it. Better still, create a JAR file for the contents of bin, put it whenever you like, and provide the absolute path of the JAR file as a CLASSPATH element when you execute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.