Need help providing parameter while batch file execution in perl script - java

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");

Related

executing SQL*Plus and copying sql file

How can I execute SQL*PLUS to execute a script. The script will generate a file, and I would like to move that generated file to a directory.
This is how the process is.
In run.sh file, I give values to parameters of a bash script
./run.sh <schema_name> <user_name> <password> <environment>
These values are passed to the sql.java:
first java goes to the following directory.
./system_scripts
There java starts SQL* PLUS and executes a script (gen.ddl) by using this command:
sqlplus <user_name>/<password>#<environment> gen.ddl
This will create a result file: result.ddl
Move (not copy) that result.ddl file to. Overwrite the previous file if any:
./schema/<schema_name>/scripts
print a message
job done
and done.
How can I do so?
thanks in advance :-)

Where to put files for a command-line Java program?

I have written a Java program that takes in arguments and then executes. I pass in these arguments from the command line (I am on a Macbook Pro using Terminal, using the bash shell). Let's say the name of my program is prgm. I want to be able to say "prgm " from any directory in the shell and then have that program execute. So I figure I need to write a bash script to reference the Java files and take in arguments, and put that bash script somewhere in my PATH. Where do I put the bash file, and where do I put my Java files? Also, am I right to assume that I only need the .class (binary) Java files?
Step-by-step:
Assuming that the name of the Java executable if myjavaprog.
Assuming that the name of your bash script is myscript.
Make sure myscript is calling myjavaprog using absolute path and the desired arguments.
call echo $PATH and you will see a bunch of paths: /some/path1:/some/other/path2:...
Put your bash script in whatever path you want from the ones returned by echo $PATH.
Go to a random path.
Call you bash script bash myscript. See the execution of myjavaprog.
Tips:
If java program is for personal use only, put it in a path starting with /usr/ or even in your $HOME directory (and add that location to your PATH)
If java program must be shared with other users, put it in an accessible place, so that other users don't need to modify their PATH variable.

How can batch file accept arguments for 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.

How to use java program as a command in cmd?

I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?
Update:
I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.
~Jacob
You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.
Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:
java -jar jarfile.jar param1 param2 ...
So for example, lets's assume that your program takes two arguments. Your script could then be as follows:
java -jar jarfile.jar %1 %2
I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?
With Launch4J you can wrap a Java program in a standalone executable file. I'm not going to copy their (long) feature list here, but definite highlights are the numerous ways presented to customize the resulting exe, its small size, the fact that it's open source and its permissive license that allows commercial usage.

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....

Categories