I'm sure this question has been asked before, but I'm at a loss here.

I've been attempting to get a piece of C++ code to work. It works perfectly on Windows AND Linux (Multiple distros tested on) however Mac is proving a pain.

I'm executing a Jar from C++, and as stated above, working on everything but Mac.

I'm using

system("java -jar myJar.jar");

I'm using this code for Mac and Linux since I'm using the WinAPI for Windows.

Any reason Mac would refuse to execute the jar?

Here is the error message I get: "Error: Unable to access jarfile myJar.jar"

Recommended Answers

All 10 Replies

Here is the error message I get: "Error: Unable to access jarfile myJar.jar"

So ... try moving the file "myJar.jar" to where it is in the path expected.

You might also check the file permissions. This is either a problem of file location or file permission, as far as I know, so just make sure both are correct.

or the filename. MacOS is case sensitive, so is Java, so make sure the filename is correct.

I'm executing all in the same directory to make sure spelling wasn't an issue. Also, permissions were changed to 777 to be sure that wasn't getting in the way either.

Try system("java -jar ./myJar.jar"); assuming that the file is in the same directory as you started your C++ application and it did not change directories before it executed the system() call.

Member Avatar for iamthwee

Interesting...

I'm assuming you have java installed on the mac, daft question but being on these forums you can't assume anything or so I've learned.

So I'm assuming you have a c++ file like:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  system("java -jar somejar.jar");
  return 0;
}

Then we must assume you compiled this on the MAC and you're not trying to run a compiled linux file. Again I have to ask this question.

So either you've compiled it with cocca or you've installed g++ for mac.

After compiling...

g++ -Wall myfile.cc

You should get a a.out file which you execute from the terminal using ./a.out Now I'm not sure if you have to change the permissions on that file.

Anyway, I can test this tomorrow with a simple jar file as I'm on a mac. I'll let you know what I find.

Nah, I compiled everything on the mac, the Java code, and JARed it. If I run

java -jar myJar.jar

within the Terminal, it runs just fine.

Try to put the full path instead of just the file name, to see if this is a problem of file-path (maybe the system function runs the command in a different directory then that of the program?).

Member Avatar for iamthwee

OK I just did a test and it works fine on mac. This is definitely a path issue.

Put the jar file and the c++ executable in the same directory then run it from the terminal.

--folder
 --myjar.jar
 --a.out

So your folder structure looks the same as above.

I did that, but no go. I did however just obtain the current directory and used that within the execution which worked. Apparently it only likes executing from the full path. So mike had the solution. Thanks all for the help. To others that run across this, use the whole path with the script being executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.