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").
Related
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.
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'm trying to write a simple batch script to test my Scala program. The script should be something like this:
#!/bin/bash
scala ./build/classes/MyClass "../../res/some_file.txt"
This returns:
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: ./build/classes/MyClass
If I'm in the classes directory running:
#!/bin/bash
scala MyClass "../../res/some_file.txt"
works as expected.
What am I doing wrong here?
-Lee
You cannot pass the file name of a class -- you have to pass the class name, and it has to be in the classpath. So, instead, try this:
#!/bin/bash
scala -cp ./build/classes MyClass "../../res/some_file.txt"
I am writing a command-line Ruby gem for CraftBukkit (of Minecraft fame).
So, CraftBukkit's .jar file functions as any command-line server interface should: it accepts user input and sends it to the server when the admin presses enter, while displaying a live feed of server events.
Executed in the command-line directly, then,
java -Xmx1024M -Xms1024M -jar craftbukkit.jar
functions perfectly. I need to execute that same command within my Ruby gem and accept user input in the same way. I am currently using %x{} like so:
%x{java -Xmx1024M -Xms1024M -jar craftbukkit.jar}
This functions reasonably well, as it displays output as it should and executes commands typed into the command line window. It does not, however, show user input as it is typed.
I need to fix this. What Ruby commands/frameworks can I use to make the java command function perfectly?
If possible, I would also like to be able to parse the output.
I am using Clamp to parse command line arguments, if that matters.
Does
system "java -Xmx1024M -Xms1024M -jar craftbukkit.jar"
do what you wanted? It should at least display the input and output. Or did you want to parse the output from within Ruby?