I got Spring boot with Spring batch project. Inside my BatchConfiguration I want to get command line arguments so I #Autowire Environment object and try to get my prop but I'm getting null.
After some debug I figured out that I can get all command line args via special property Name "nonOptionArgs" but in this case I got plain string of all arguments passed. Is there some better solution?
Thanks
You are doing everything right with autowired environment. Make sure you pass arguments in the command line with "--"
From documentation:
By default SpringApplication will convert any command line option
arguments (starting with ‘--’, e.g. --server.port=9000) to a property
and add it to the Spring Environment. As mentioned above, command line
properties always take precedence over other property sources.
you can run your app like this:
$ java -server -Dmyproperty=blabla -jar myapp.jar
and can access the value of myproperty in code.
Related
I am trying to pass named arguments to launch.json as program arguments in VSCode.
--myArgument=My Argument Value
So that I can catch this named property in Spring Boot as an external property.
I have tried the "args" key but this passes them to the main function.
Is there a way to achieve this in the VSCode Java Debugger?
This is my first time working on a spring batch project. So I need to know how to pass arguments to the main class in a .sh file. The project is currently working and .sh file has a command line like this:
java -Xmx2048M -jar somebatch-SNAPSHOT.jar applicationContext.xml
And I want to pass arguments by changing this command. the project runs all jobs at the main class so job names are not specified in the command. What I want to do is pass arguments so I can select which jobs will run. Something like this:
java -Xmx2048M -jar somebatch-SNAPSHOT.jar --argument 1 applicationContext.xml
I don't need to pass the arguments to the jobParameters like java -jar somejar.jar somejob value=1 as shown in some examples. I only need a value in the main class.
Any help is appreciated.
I don't need to pass the arguments to the jobParameters like java -jar somejar.jar somejob value=1 as shown in some examples. I only need a value in the main class.
In this case, you can pass parameters on the command line with -Dparam=value and then get them in your main class using System.getProperty("param").
I am using hadoop jar a.jar com.class.Name -Dkey=value arg1 arg2.
In the Main class when using args, I get the -D as arg value. Isn't Java supposed to ignore it?
According to hadoop commands manual:
Generic Options
The following options are supported by dfsadmin, fs, fsck, job and fetchdt.
Applications should implement Tool to support GenericOptions.
-conf Specify an application configuration file.
-D = Use value for given property.
-jt or Specify a ResourceManager. Applies only to job.
I am having a spring batch application, that is invoked through the command line. I would want to put that invocation in a shell script, so that I can run a script rather than the entire command.
For example, my invocation looks like:
java -jar run=1
The problem is for each run, the job parameter needs to be incremented. Is there a way through which i can achieve that in a shell script?
Thanks
You need an incrementer for this as usual.
<bean id="simpleIncrementer"
class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>
<job id="myJob" incrementer="simpleIncrementer">
</job>
The trick for this incrementer to work with CommandLineJobRunner is adding the -next parameter when running the task.
-next: (optional) to start the next in a sequence according to the JobParametersIncrementer in the Job
Something like this:
java –jar myjob.jar jobs/myjob.xml myjob -next
You can do it n times (in a terminal) using a for loop this way:
for i in {1..10}; do java -jar run=$i; done
I'd like to put some Scala scripts into a batch file. If you just print the arguments, the canonical way would be like this:
::#!
#echo off
scala %0 %*
goto :eof
::!#
args foreach println
(This actually calls another batch script, scala.bat from the bin-directory of Scala). If I try to pass an argument containing parentheses to it, the windows command line interpreter complains with a syntax error:
printargs.bat "foo(bar)baz"
Strangely, if I create a scala file printargs.scala containing just args foreach println, this works correctly:
scala printargs.scala "foo(bar)baz"
So I assume this is not a bug in scala.bat. But what can I do?
Update: the actual error occurs in the following line in scala.bat:
set _ARGS=%*
with the error message
baz was unexpected at this time
If I change scala.bat that it does not use a local variable but use %* directly, it works correctly. So I filed a bug-report. The question remains if there is a workaround for this problem until the bug is fixed.
This bug does not occur in scala 2.8.1.final.
I would suggest using that version instead.
I don't think this has anything to do with Scala.. I'm currently having the same problem with passing an argument which contains parentheses into a grails command. Apparently this is a problem with the windows shell, have tried escaping the characters "(" but that does not work :/