How to get my .bat file to run a java command - java

I want to run a simple batch file that navigates to a folder location and runs a java command. What I have now won't run the command.
#echo on
set /p DIR="C:\Application\dir_to_run_from"
dir %DIR%
java -cp file.jar com.myCompany.db.collector.Collector
Right now I'm just getting the path printed when I run the command.

set /p does not set the path. p stands for prompt. Usually, you'll use it like this:
set /p ANIMAL=Enter your favorite animal:
Instead, you probably just want to replace the first line with this:
cd "C:\Application\dir_to_run_from"
This will cd (change directory) to the directory you want to use. (You should change the second line to just dir if you take this route.)
Alternatively, you could replace the last line with this:
java -cp %DIR%\file.jar com.myCompany.db.collector.Collector

Related

Need help running my Java .jar file from Windows batch file

I have a Java jar file located in:
C:\Users\myusername\bin\MyDir\MyApp.jar
I also have some required properties files (needed as input arguments to the .jar file) located in the same directory as the .jar file.
I created a runme.bat file here:
C:\Users\myusername\Desktop\runme.bat
In the runme.bat file, this is what I have:
setlocal
set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\bin\"
set PATH=C:\Users\myusername\bin\MyDir\
start %JAVA_HOME%javaw -jar %PATH%MyApp.jar %PATH%propertiesfile.properties
However, whenever I try to run the .bat file, I get the error:
Windows cannot find '-jar' Make sure you typed the name correctly, and then try again.
On the command line I see Windows trying to do this:
> "C:\Program Files\Java\jdk1.8.0_161\bin\"javaw -jar C:\Users\myusername\bin\MyDir\MyApp.jar ...
I get this error when running from the command line. If I simply double-click the .bat file, a cmd window comes up and quickly disappears.
So, what am I doing wrong?
Thanks!
Use Double quotes around the set command, not inside the variables.
Also, I see no reason to use the START command unless you want to do more in your batch file in the original command prompt after starting your Java in a second command prompt. Possible but seems unlikely.
Generally, you will just type in the executable or use CALL so that the executable runs and control returns to the batch after reaching conclusion.
Additionally, you changed your system path variable to be just the path of your java files which will make the session pretty screwy. Thankfully this should only persist in your open command windows and those spawned by the original window, so close them all and then use a different variable name for your path.
So I will put this both ways, using Call, and using start.
Here is your code using call:
#(
setlocal
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
TITLE "%_Title%"
CD /D "%JAVA_HOME%"
CALL "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%\propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
Here is your code using the start command:
#(
SETLOCAL
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
start "%_Title%" /D "%JAVA_HOME%" "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)

"No such file or directory" when running java from shell script

I'm trying to run a script from an Amazon Linux machine. The script invokes checkstyle like this (in a script called eval.sh):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. Both checkstyle-6.15-all.jar and checks.xml are where they should be.
What could cause this problem?
Change "${CHECKSTYLE}" to ${CHECKSTYLE} (without the quotes).
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar.
When you envoke "${CHECKSTYLE}" the shell thinks that is the command you are running. There is no such file name with the spaces and options have you have included there. If you envoke it simply as ${CHECKSTYLE} (drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process.

Launch jar with custom command line program

Suppose I have some folder in a directory:
- MyApp
- lib
- myapp.jar
The location of the MyApp directory is supposed to be stored in an environment variable, like APP_HOME. I would like to add a bin folder that contains two commmand-line executables that launch the java program, one for Windows and one for Unix-based OSs. I already know that one file would just be called myapp and modified with chmod +x, and the Windows one would be named myapp.bat.
What I am unsure about is what the contents of these files would be. As said, both would run the jar file with a custom command line command whose arguments are passed to the main method, as shown below:
>myapp -debug key=value moreargs...
EDIT: How would I go about creating this environment variable, from Java code?
You can pass command line arguments to the executable by adding $* at the end of the command in the Unix shell script, and %* for the Windows batch file:
java -jar $APP_HOME/lib/myapp.jar $*

"C:\mywork" is not recognized as an internal or external command, operable program or batch file

Learning some basic Java and one task is trying to open a .java using the Command Line. I've got a .Java located in a new folder at "C:\mywork" Trying to first navigate to the folder but im getting that error.
Thanks
You need to use the cd (Change Directory) command:
cd c:\mywork
Then you can use the dir command to list the contents of the directory. Refer to this tutorial on the windows command line: http://www.cs.princeton.edu/courses/archive/spr05/cos126/cmd-prompt.html
use cd C:\mywork if c: is the default drive, otherwise first enter C: in cmd and then enter cd C:\mywork . Then you can continue with javac Sample.java and java Sample etc

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.

Categories