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?

CodeMed
  • 9,527
  • 70
  • 212
  • 364

1 Answers1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483