Gradle provides a handy application plugin which you can use like this
apply plugin: 'application'
mainClassName = "foo.bar.Baz"
then invoke "*dist" tasks and get some scripts generated to run the app
bin/
app
app.bat
lib/
...
however when i run sh app -DmyConfig=myValue the application won't get a jvm argument by name myConfig.
So how do we pass arguments trough command line?
All parameters you give the default launch script generated by Gradle are passed on like they are to the main method of your program, but there are some environment variables you can set, or you can do the setting in your main method.
So either you parse your args in main and set the system properties from your code, or you set environment variables. The default Gradle-generated launcher scripts consider three environment variables that it adds together. DEFAULT_JVM_OPTS which is set within the launcher script, JAVA_OPTS which is meant for all Java programs that respect this property and one additional environment property that is specific to your program. Just look inside the launcher script to find out its name.
As third option you can of course also use an own launcher script that does special handling of parameters that start with -D and sets them as system properties instead of giving them to your programs main method.
As your question suggests that you are working in a linux shell, one way to set an environment variable just for one call of an app would be YOUR_APP_OPTS="-DyourProp=yourVal" ./yourApp. But any way to set an environment variable would be fine.
Related
So basically i have a project with a lot of packages and the corresponding jars.
im trying to run the program and to do so i need to set my CLASSPATH the thing is when im trying to set it it says command not found for some reason.
What i did was:
martim#localhost:~/project/woo-app>CLASSPATH = /usr/share/java/po-uulib.jar:/home/martimc/project/woo-app/:/home/martimc/project/woo-app/
Bash : CLASSPATH: command not found
I dont understand why this is happening
Setting environment variables in bash requires no spaces before or after the =. Furthermore, you'd have to put export in front or it won't actually provide that var to java. Lastly, don't use CLASSPATH at all; java can run many apps, apps need different classpath, so the concept of a global classpath makes no sense. Just use the -cp parameter.
My java application uses environment variables (System.getenv), and this set of variables is large and defined in a file like:
export VAR1="VAL1"
...
export VAR100500="VAL1500"
when i start the application from command line i use something like this:
source ../.config/.secret-secret && java -jar build/libs/app-name.jar
and the values are exported right before the launch (actually, the source command may be executed only once per session, since the values are exported)
But when i start the application from IDEA, reasonably those values are not exported to the run environment.
So, the question is: how can i export a set of environment variables to application launch environment in IDEA?
So, what i've tried already:
in Run/Debug Configurations -> Before Launch i started a shell
script which exports those variables (i use the same script to run
the app from command line). Script executed successfully, but looks
like the script is executed in different environment from the
application itself, so values are exported (and printed to IDEA run
console), but the application still can't read them from
System.getenv
i can't easily use Run/Debug Configurations -> Environment Variables, because there are a lot of variables to set up manually, also they are changed from time to time.
It's not enough to run entire IDEA with those variables, because, as i mentioned above, the values are changed often and should be re-read almost every launch.
I can tell you that editing the workspace.xml file (which is where the environment variables entered in the run configuration are stored) directly does not work, so don't bother trying that.
I think you are going to have to write a plugin that parses your script and updates the run configuration. I haven't written a plugin to do this myself so can't give you a fuller answer. You can see the relevant plugin in documentation here
When I run the following command:
bin/tdbloader2 --loc=/store/data/here /seed/data/serverfault-dump.nt
And the response I get is:
10:52:31 -- TDB Bulk Loader Start
10:52:31 Data phase
Error: Could not find or load main class com.hp.hpl.jena.tdb.store.bulkloader2.CmdNodeTableBuilder
Which is most likely caused by a problem with my environment variables, somewhere, of something. The problem is, I don't often work with Java and so I don't know enough to know how to figure out what that class cannot be found.
How do make tdbloader2 find the appropriate class?
I had forgotten to set the JENAROOT path variable.
https://jena.apache.org/documentation/tools/
An environment variable JENAROOT is used by all the command line tools to
configure the class path automatically for you. You can set this up as follows:
On Linux / Mac
export JENAROOT=the directory you downloaded Jena to export
PATH=$PATH:$JENAROOT/bin On Windows
SET JENAROOT=the directory you downloaded Jena to SET
PATH=%PATH%;%JENAROOT%\bat
At a minimum you need to set the TDBROOT environment variable to the directory containing your TDB download. This will be the directory above bin as some of the scripts use ${TDBROOT}/bin/foo to launch other scripts
The script attempts to automatically construct a valid class path by calling the tdb_path script which calls either make_classpath_mvn or make_classpath depending on your environment. If you run the tdb_path script directly you can see if it generates a sane looking class path or not (or add what it does generate to your question if the output doesn't provide anything helpful).
I'm doing essentially the same thing as the original poster of this question. In my case, I'm trying to run the Sun/Oracle JPDA example programs in Eclipse Kepler on OS X 10.8.5, with Oracle jdk1.7.0_72. However, the documentation for those examples seems to assume they will be run from the command line.
I want to use com.sun.jdi.connect.LaunchingConnector to launch the debuggee program. It requires arguments of class Map <String, Argument>. One of the map entries has key "main" and its associated object contains a String that is the debuggee main class name. In my case, that is "debuggee.DebuggerTest". The program that invokes the launch has main class "debugger.TraceLaunch".
My Eclipse project has the default structure, so there is a folder named "classes" with a subfolder for each package. In my case, those are "com", "debugger", and "debuggee". The "com" package includes all the classes from "Example JDI Applications".
I am using "VMLauncher", referenced in the post cited above, to do the launching. The launch fails; it reports that my main class "debuggee.DebuggerTest" cannot be found. It also fails if I try to include the full path as part of my main class name.
If I open a terminal and export a CLASSPATH variable that specifies the "classes" folder inside my Eclipse project (and the JDI library), I can launch the debuggee with the command "java debuggee.DebuggerTest" and it runs correctly. I can also launch the debugger with the command "java debugger.TraceLaunch", and it in turn successfully launches the debuggee. This suggests that VMLauncher is doing all the right things.
So it seems that the problem is that somewhere deep in the examples packages or the JDI packages from Oracle's "tools.jar", the LaunchingConnector invokes a command line launcher. It seems to be of class SunCommandLineLauncher. That launcher seems to assume a classpath. The launcher arguments don't seem to be documented, so I don't know if there is an option to specify a classpath. The source code for the Oracle jdk1.7.0 JDI classes (in "lib/tools.jar") has apparently not been published, so I can't look at the code for the details of how the launcher uses its arguments.
One other clue that may be helpful: in the example JDI applications, the GUI debugger example allows specifying a classpath, which it tries to parse. Unfortunately, it does not recognize quoting or character-escaping like a UNIX shell does, and it does not understand folder names that include a space character.
Ideally, I'd like to know how to specify a classpath to a LaunchingConnector.
My second choice would be some more general suggestions for how to specify a classpath when adapting a command line program to run in Eclipse.
The new VM which is created by the Trace has different class path. If you want to run the program in eclipse you need to add the following code in your trace program.
Connector.Argument options =(Connector.Argument)arguments.get("options");
options.setValue("-cp \"F:\\Workspace\\TraceProgram\\bin\"");
Where -cp set the class path to newly created VM, you need to change to your eclipse work directory.
It is worked for me.
I would like to use a run configuration (or something similar) to run a class from Eclipse using a shell script. The shell script will do a bunch of fancy stuff to make the job run on a machine with more memory. That part I can do.
What I don't know is how to make Eclipse pass the class name and class path to a shell script - and ideally show the output of the shell script in the console window. This seems like it should be simple. I'm using 3.5.2.
Thanks!
I'm not sure of a clever way to pass your classpath to your script, but the External Tools Configurations can do this. And the output will be printed in your console. There are Variables that can be set up to manage your classpath a bit better than I have done below. Though I've used the working directory as my classes folder, so I only need to use . as my classpath.
Variables can be used throughout to set up common paths etc.