How can batch file accept arguments for Java? - java

I'm trying to write a batch file (run.bat) that can be invoked something like this:
run.bat "whatever.log"
But under the hood, the batch file is passing the argument ("whatever.log") to the following command:
java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver "whatever.log"
Again, if you run: run.bat "blah.txt", then that batch file would execute:
java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver "blah.txt"
My best attempt at run.bat so far is:
#ECHO OFF
%JAVA_HOME%\bin\java java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver ???
But I'm not sure how to parameterize the argument (???). I'm also not sure if the batch file is missing anything or is incorrect with the way I've written it. Ideas? Thanks in advance!

You just need to put %1, but you have another issue. When you use 'java -jar', the '-cp' argument is ignored: the CLASSPATH is taken from the jar manifest only.

To pass one command line parameter, use %1. If you need to pass more than one, you can either use %1 %2 etc, or use %* to pass all of them at once.

Related

Using parameters from shell script in my Java code

I have to write two scripts, one to compile my code and another one to run it. I manage to compile the code with this script:
#!/bin/bash
javac SimilaridadeP.java
And I was able to run it using this script:
#!/bin/bash
java SimilaridadeP
The problem is that I need to execute my second shell script following this command:
./execute.sh input output
Where input and output are the names of the files that I am going to use in my Java code. The problem that I am facing right now is that I don't know how I can use these both names in my Java code.
So far I managed to get both names using this code:
#!/bin/bash
in=$1
out=$2
java SimilaridadeP
But since "SimilaridadeP" is the name of my java file and it can't be called like a method with parameters I am kind of lost.
If your java code is already reading the command-line parameters, it's just a matter of changing the bash this way:
#!/bin/bash
java SimilaridadeP "$1" "$2"
This works with arbitrary number of arguments:
#!/bin/bash
java SimilaridadeP "$#"

Need help providing parameter while batch file execution in perl script

I have to execute a abc.bat file in a perl script.
If I run the batch file manually it asks for first param at first step. When I provide this at second step it will ask for second param..
I need to call this abc.bat file in a perl script
Something like..
system(‘abc.bat’); or exec()
But how to provide param run time..
The abc.bat batch file is a calling a java myclass which takes two params at runtime.
Below is the batch file
#echo off
setlocal
set classpath=.\my.jar;%classpath%
"%JAVA_HOME%"\bin\java com.myclass
#echo on
Please help .. Thank you.
Can you just use the %ENV variable to do everything directly in perl? Maybe:
$ENV{classpath} = ".\\my.jar;$ENV{classpath}";
system("$ENV{JAVA_HOME}\\bin\\java com.myclass");

Executing a java class in the linux terminal without the java command

I have a java class, cs.class, that I would like to execute from the command line, just as you use any other command. I would like to be able to type 'cs file1' etc. Without having to use 'java cs file1'. How can I do this?
edit: I would also like this to work if I put my class anywhere in my path.
It looks like this will work for you on linux.
For Windows
Create a file named custom.cmd and add the following to it
#echo off
DOSKEY cs=java cs $*
Now
right click your command prompt shortcut->properties->shortcut tab->and append the following to your target field
/K C:\custom.cmd
my custom.cmd resides in C:, change the path to yours
Now you can use "cs" as a command within that cmd shell. You can mention you filename as an argument as well as $* specifies command line arguments.

Java Filepath Question

I am trying to finish a java program that uploads a file from a client machine to a webserver. The java program is executed with a bat script. I need to pass in a file name to the java program somehow since the filename is different every time. Or can i somehow use %1 instead of the filepath? I dont know.
Why not simply forward the parameters passed to the shell script to the Java application. I usually do something like this:
#!/bin/zsh
java -jar someapp.jar $#
This will pass all the arguments with which the script was executed to the java app and you can act upon them - as far as I understand you need only only - the file path. I'm not familiar with bat scripts, but I assume they have some similar way of passing args around.
What does the batch file look like that runs the Java program? You can indeed use parameters like this:
java -jar program.jar %1
If you put that line in a file runprogram.bat, then you could run it with:
runprogram somefilename.xyz
and somefilename.xyz will be passed to the Java program as a command line argument.
No they got it, if i could just pass the filepath as a parameter to the executed jar that would be awesome. Just need to figure out how to pass that parameter into a variable in the program....

How to pass Properties to jar from Powershell?

I've been using Powershell-1.0 for command line needs for a while, instead of cmd.exe. Unfortunately, there are still some caveats when using Java. I need to pass a property to a jar, like that:
java -jar -Duser.language=en any.jar
This line works fine in cmd.exe, but not in Powershell as it searches for another jar:
Unable to access jarfile user.language=en
Using quotes doesn't help.
Is it doable in Powershell-1.0, or do I miss something in Java?
Take a look at my answer to this question. Note how you can use echoargs.exe to diagnose these sort of problems. Most likely the fix would be to quote the parameter e.g.:
java -jar "-Duser.language=en" any.jar
You can test that using echoargs (from PowerShell Community Extensions):
echoargs -jar "-Duser.language=en" any.jar
Arg 0 is <-jar>
Arg 1 is <-Duser.language=en>
Arg 2 is <any.jar>
Using quotes works fine for me in PowerShell on Windows 7.
java "-Dmy.property=value" -jar myjar.jar
Be careful: the jar name must be placed right after -jar, and arguments placed after -jar myjar.jar will be passed to the program inside the jarFile.
Try launching instead using the following pattern:
java -Duser.language=en -jar any.jar
That assumes that user.language is meant as a system property. If you meant it as a command line argument, change that to:
java -jar any.jar -Duser.language=en
I am actually surprised that the command line you mentioned works at all outside of powershell (though I have confirmed that it works fine for me too, even on Linux) and it is also a little strange that things would work differently inside and outside of powershell.
From java -help:
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
...
-D<name>=<value>
set a system property
...
So basically you should always put the JAR filename directly after the -jar command line option, and any JVM options (such as setting system properties with -D) before.

Categories