Intellij IDEA: run/debug java application with a set of environment variables - java

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

Related

How to pass jvm arg to java app distribution created with gradle

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.

Why won't my batch files run since I changed a Windows "Environment Variable"?

I had a couple of batch files to help me with minor tasks (one killed a running Skype process and the other deleted a file before running an executable). Both were only one or two lines of simple code.
To run a program called Kernow I was told I needed to add a Windows environment variable called "Path" and set it to point to a Java folder (C:\Program Files (x86)\Java\jre7\bin) - the program wouldn't run until I had done this giving a Java error.
I already had an environment variable called path which pointed to a long file path so instead of adding a variable I simply updated that one.
Now neither of the batch files that were working fine before will work. Both will run with no errors but not produce results.
I have very little knowledge of how batch files work with Windows. Can I set another environment variable called Path that will enable me to run my batch files and run Kernow??
You probably messed up with adding the folder to your path.
Your path should look like this:
C:\Folder1;C:\Folder2
When you add Java, you need to append it and add a semicolon, like this:
C:\Folder1;C:\Folder2;C:\Program Files (x86)\Java\jre7\bin
You may have missed the semicolon or completely overwritten the path.
An easy command to do this, as mentioned by #unclemeat in the comments:
set path=%path%;C:\Program Files (x86)\Java\jre7\bin

How to run database program outside the Netbeans IDE?

I am really new to database and this is my first program in database using java Netbeans 7.1 --- It is summer on our country now and I am a student with the course I.T. Our next subject on programming is about database and since there's no class I spend my time learning database for preparation for the next school semester and I refer to this site as my guide for the first database program I currently making now.
http://www.homeandlearn.co.uk/java/databases_and_java_forms.html
I did everything in the tutorial and I actually done doing the program.
The final thing I did is I clean and build the program since I want the program to run with out opening the netbeans again I downloaded the JRE and make my database_form.jar as a jar executable. "database_form" the name of my Netbeans Project. I do that by making javaw in JRE.7 as my dafault when opening any jar files.
Anyway, this is how i run the program.
Running Program in Netbeans IDE
Using Netbeans, before my program works I first need to "Start Server" on JavaDB. because if I didn't do that an Exception occurred "Err. connecting to server localhost 1527 and so on"
Running Program using jar executable alone.
The Problem is there's an Exception and Err in connecting still occurred.
What I want to achieve?
I want the program to run without opening the Netbeans IDE and going to Java DB to click the "Start Server", I dont want to do that anymore. Or my second option is to start the server using command prompt so that I just have to make a bat file so that whenever I open my program database_form.jar I just place the bat file on my desktop and run it.
Second Problem!
Actually, I already try my second option by using command prompt to start the server but I forgot how I did it, I just found it on some website the only thing I remember is the Exception says "Failed to lunch the server because the database Employees is missing. Employees is the name of my created database.
The O.S. I am using is Windows 7.
Thank you for all the reply and sorry for the long text I just want to be specific, :D
Right, from your description there seems to be a couple of things you are confusing.
First, databases are typically run as servers with multiple clients connecting to them thus allowing they contain to be shared. When you start Java DB you are starting the Java DB database server.
That said, lightweight databases, such as Java DB can be run in an embedded mode as explained here. Remember that the directory you point to with the derby.system.home property will need to contain the database files, if not you'll need to create that programatically too.
Second, there's various ways to run a Java application outside of an IDE, but jars themselves are not executable in the same way an exe file is in Windows.
The simplest way is to call the java executable passing the necessary classpath and the name of the class containing the main method. For example if I had a class called com.example.Application that had been compiled to a directory C:\dev\example\classes the following command line would run the application:
java -cp C:\dev\example\classes com.example.Application
If there were dependencies on external libraries, as there will be in your case on the Derby JDBC driver, then those would also need including in the classpath resulting in something like:
java -cp C:\dev\example\classes;C:\dev\lib\derby.jar com.example.Application
There's a full set of document on the Java launcher here.
Now, back to the jar. Like I said, jars are not executable but there is something that's know as an 'executable jar'. This is the same as any jar except there are some special additions to the manifest to specify the application entry point or main-class and the class-path as described here.
Once the main-class and class-path are specified in the jar's manifest, the following command line would run the application:
java -jar C:\dev\example.jar
You can even associate the jar extension with the java exe and double clicking the jar will cause the application to launch (though on a dev machine it's probably more useful that the jar extension be associated with WinZip or simlar in order to open that jar).
The default database in Netbeans is Derby/JavaDB. So you need to:
add the jar of javadb/derby in our classpath (it maybe already present, as it is bundled with java when you install it in Ubuntu)
setup a path with the jdbc URI to save the database data
I personally recommend the usage of hsqldb or H2 for this: they support in-memory database, very useful for stand alone project with no persistence data or for tests.
If you use window, add ODBC Data Sources from Administrative Tools to your Java Derby driver and run jar.

Eclipse run configuration to run remotely using a script

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.

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Categories