2

This is my code for a program that creates an environment to run a Java .jar file. I am using the _execl function and am programming on Windows:

#include <stdlib.h>
#include <stdio.h>
#include <process.h>

int setenv(const char *name, const char *value, int overwrite);

int main()
{
    setenv("JAVA_HOME2", "path\\to\\java.exe", 1);
    char *var, *param, *path;

    var = getenv("JAVA_HOME2");
    param = ("-jar");
    path = ("path\\to\\foo.jar");

    intptr_t _execl(var, param, path, NULL);
}

int setenv(const char *name, const char *value, int overwrite)
{
    int errcode = 0;
    if(!overwrite) {
        size_t envsize = 0;
        errcode = getenv_s(&envsize, NULL, 0, name);
        if(errcode || envsize) return errcode;
    }
    return _putenv_s(name, value);
}

However, VS Code returns the error: expected ')' before '(' token on line 16, which is intptr_t _execl(var, param, path, NULL);.

Does anyone have any solutions?

rioV8
  • 24,506
  • 3
  • 32
  • 49
Abstrogic
  • 37
  • 2
  • `intptr_t _execl(var, param, path, NULL);` is nonsense. that `intptr_t` has no business being there. It seems you either forgot the `var = ` or copy/pasted a prototype, then filled in the blanks without trimming the return type. And fyi, I removed the `java` tag you had linked to this post; clearly this has *nothing* to do with Java when its a C compilation error. – WhozCraig Aug 28 '21 at 02:20
  • save the compilation after the preprocessor sometimes helps to understand a vague error message, look in your compiler docs what the CLI option is. – rioV8 Aug 28 '21 at 03:09

1 Answers1

1

You're getting an error here:

intptr_t _execl(var, param, path, NULL);

Because this is not a function call but a declaration of a function.

By putting a type name before the function, you're declaring that _execl is a function whose return value is of that type. The parameters given to the function are then taken as the names of those parameters, and since NULL is not a valid identifier for a parameter you get an error.

If you look at the several other places you call a function such as setenv and getenv you're not putting a type name first. So remove it:

_execl(var, param, path, NULL);

And now you have a function call.

This still won't call the function properly, however. The first argument is the path to the executable, and the remaining arguments are command line arguments to the program, and the first command line argument is the name of the program being run. So you actually want to call it like this:

_execl(var, var, param, path, NULL);

Or this:

_execl(var, "java", param, path, NULL);

Also, there's no need for parenthesis around a string constant when you assign it to something. So this:

param = ("-jar");
path = ("path\\to\\foo.jar");

Can be replaced with:

param = "-jar";
path = "path\\to\\foo.jar";

Or you can get rid of these temporaries entirely and pass the strings directly to _execl:

_execl(var, "java", "-jar", "path\\to\\foo.jar", NULL);
dbush
  • 205,898
  • 23
  • 218
  • 273