Home > Software design >  Node spawn transforms arguments characters?
Node spawn transforms arguments characters?

Time:11-06

This is how I am using function spawn from node:child_process package:

const args = [
    'Djava.library.path=./DynamoDBLocal_lib',
    'jar ./DynamoDBLocal.jar',
    'inMemory'
]

const dynamodb = spawn('java', args, {cwd: './dynamodb_local'})

It looks like the path from the first argument, gets somehow changed along the way because stderr from this command logs this

Error: Could not find or load main class Djava.library.path=..DynamoDBLocal_lib
Caused by: java.lang.ClassNotFoundException: Djava/library/path=//DynamoDBLocal_lib

It looks like the slash gets converted to a dot and vice-versa?

This command, when used normally in a shell, works as expected.

Edit: I am running this on macOS.

CodePudding user response:

You said it works fine in a shell, so I suggest you use the shell option (documentation). Just make sure not to pass unsanitized user input into it. You can do it like this:

const args = [
    '-Djava.library.path=./DynamoDBLocal_lib',
    '-jar ./DynamoDBLocal.jar',
    '-inMemory'
]

const dynamodb = spawn('java', args, {cwd: './dynamodb_local', shell: true})

Note that with the shell option enabled, you'll need to add dashes to your arguments.

  • Related