# Custom classpath for a CommandBox server instance

Think I asked this before a long time ago, but might as well check for current CB. Is there any way to define a custom classpath for a CommandBox server instance? An app I work on has a lot of Java, and as you probably know, JavaLoader isn’t a completely transparent solution at scale, so we actually need to add our dir to the classpath for the server instance. Simple for static servers, would be super awesome if you could do that for CB instances.

Thanks.

You should be able to do that with the jvmargs parameter on the server start command. I haven't tried it myself but I think that's your best bet.

Server start jvmArgs=

I'm not an expert so don't take my answer to the bank.

confirmed: jvmArgs worked for me. you just give it the same arguments as the java.exe command. (-cp or -classpath for setting classpath) :



CommandBox:myv> server start JVMargs="-cp ."
Contacting ForgeBox to determine the best version match for [10]. Use an exact ‘cfengine’ version to skip this check.
The server for C:\webroots\myv is starting on 127.0.0.1:50635… type ‘server status’ to see result
CommandBox:myv> server show
{
“app”:{
},
“verbose”:true,
“JVM”:{
“args”:"-cp ."
}
}
CommandBox:myv> server status --verbose

myv (running) 127.0.0.1:50635 → C:\webroots\myv

Starting in background - Server is up - http-port:50635 stop-port:55952 PID:8256 version 3.4.10

-cp .
-Xmx512m
-Xms512m
-jar “C:\Users\user.CommandBox\lib\runwar-3.4.10.jar”
–background=true
–port 50635
–host 127.0.0.1
–debug=false
–stop-port 55952
–open-browser true
–open-url http://127.0.0.1:50635
–server-name “myv”
–tray-icon “C:\Users\user.CommandBox\cfml\system\config\server-icons\trayicon-cf10-32px.png”
–directoryindex “true”
–cfml-web-config “C:\Users\user.CommandBox/server/F072138B9E86DC43FD4DC1C70F3B5CAF-myv”
–cfml-server-config “C:\Users\user.CommandBox/engine/cfml/server/”
–timeout 240
-war “C:\webroots\myv”
–urlrewrite-enable false

CommandBox:myv>

Yes, use the libDirs parameter to the start command (or the corresponding server.json setting). This is a comma-delimited list of paths to folders that contain jars you wish to be class loaded into the application.

Thanks!

Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

While setting the class path will work, it’s a bit hacky. Just use the libDirs parameter as it’s intended for this purpose.

Thanks!

Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Hi folks, taking a look at this again after a long while.

First off, just to confirm my understanding, the server.json file at the root of the server instance is used by default, yes?

Second, to add a directory to the classpath, my understanding is that it should include something like this::

“libDirs”:"\path\to\my\jars\"

Is that right? Does it go at the root of server.json?
And the paths listed should relative to the server root?

Third, is it possible to add mappings via server.json? (Adding them through Application.cfc isn’t possible for reasons not worth going into here.)

Thanks again for any assistance,
Dave

First off, just to confirm my understanding, the server.json file at the root of the server instance is used by default, yes?

Yes, as documented here: https://commandbox.ortusbooks.com/content/embedded_server/serverJSON/serverjson.html

Second, to add a directory to the classpath, my understanding is that it should include something like this::

“libDirs”:"\path\to\my\jars\"

Sort of. You are correct that backslashes need to be escaped, but a path like that looks like a *nix path, which would use forward slashes. A path starting with a slash is assumed to be absolute so the path you have above on Windows would resolve to the root whatever drive letter was on your current working directory. Perhaps that’s what you wanted, but I assume not.

Does it go at the root of server.json?

No, it goes under app.libDirs as documented here: https://commandbox.ortusbooks.com/content/embedded_server/adding-custom-libs.html
Pro tip: use the CLI to set these with the “server set” command. Hitting tab will show you all the possible options.

And the paths listed should relative to the server root?

It depends. An absolute path is, well absolute! A relative path passed to a CLI param will be relative to the current working dir. a Relative path in a server.json will be relative to the directory that the JSON file lives in. (Note, server.json doesn’t be in the web root). A global server default config setting will be relative to the web root. So in your case, the answer would be yes if your server.json is in the web root.

Third, is it possible to add mappings via server.json?

No, but not to worry, we have another project that manages that and more called CFConfig.

https://www.ortussolutions.com/blog/introducing-cfconfig-a-new-way-to-manage-your-cf-servers-configuration-from-the-command-line

That project will not only allow you to list/add/edit/delete CF mappings from the command line, you can package a .cfconfig.json file that has your CF mappings in it along with datasources, mail servers, timeouts and anything else that can be set in the CF administrator.

Thanks!