0

I have a little C program where I am forking a new process and I want to execute a Jar file there. This is the exec call:

execl("/usr/bin/java", "-jar", "/home/user/path/file.jar", NULL)

But I get an error:

Error: Could not find or load main class .home.user.path.file.jar

If I run the same command from command line the Jar runs without problems. I already checked the manifest file, everything is fine there.

So I have no idea why the slashes in the path are transformed into dots, leading to the error. Can anyone shed light on this issue?

flowit
  • 1,382
  • 1
  • 10
  • 36
  • It's OK with the dots, as Java uses dots to identify packages. You need to provide a Main Java class inside the jar (i.e., the starting point of the application) in order to have the jar "running". It seems that there is no Main class in that jar, or that there's more than one (and the JVM doesn't know which to use). – javatutorial Oct 05 '15 at 10:59
  • This is set in mainfest, "everything is fine there"? – Petter Friberg Oct 05 '15 at 11:02
  • In my manifest: Main-Class: main.MyClass In my source file: package main; The source file is in the directory main. .home.user are NOT packages, it should be the absolute path to the jar file – flowit Oct 05 '15 at 11:06
  • The maifest is very sensibile of spaces after classpath, make sure it execute s without your program or check out this to provided class name with main metodo: http://stackoverflow.com/questions/32861000/unable-to-launch-jar-with-execl – Petter Friberg Oct 05 '15 at 11:10
  • Are you sure when your run it from command line you are running exactly the same thing? You are at the same working directory, you invoke both java and the jar as absolute paths? – RealSkeptic Oct 05 '15 at 11:12
  • Yes I am sure. Petter and Ture were absolutly right, I forgot argv0. Still dealing with "Unable to access jarfile" error, although I already set the permissions to 777. – flowit Oct 05 '15 at 11:22

1 Answers1

1

Notice that execl takes "path" and "argv0" as two separate arguments, even though one usually thinks of them as the same thing. I.e, you typically need something like

execl("/usr/bin/java", "/usr/bin/java", "-jar", "foo.jar", NULL);
Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15