cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java folser.folder1 ../Files/MySQL.xml
cd ..
I need to run the batch file from any directory. I have set the paths for java. Can anybody help me?
Under *nix (e.g. Linux):
cd "`dirname \"$0\"`"
# your current directoy is now the script's directory
cd ../../jobs
set CLASSPATH=.:../xyz.jar:../mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ../Files/MySQL.xml
cd ..
# when the script terminates, you are automatically
# back to the original directory
Under Windows NT/XP/etc.:
SETLOCAL
PUSHD .
REM current directory has been saved and environment is protected
CD /d %~dp0
REM your current directoy is now the script's directory
CD ..\..\jobs
SET CLASSPATH=.;..\xyz.jar;..\mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ..\Files\MySQL.xml
CD ..
REM before the script terminates, you must explicitly
REM return back to the original directory
POPD
ENDLOCAL
Although I can't comment on Vlad's answer (comments require more points than answers?!) I would always be wary of relying on:
CD /d %~dp0
because Windows can't CD to UNC paths and has a nasty habit of dropping you into %windir% instead with potentially catastrophic results.
Instead, although it is more long-winded, you are usually better off referring to %~dp0 (or a variable containing that value) each time you need a full path.
BAD:
cd /d %~dp0
rd temp
GOOD:
rd %~dp0\temp
You message was a bit garbled, I'm assuming you're saying that java is on the path but you can't properly run your application from a batch file. It looks like you are missing the classpath option (-cp) for java. Try this:
cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java -cp %CLASSPATH% folser.folder1 ../Files/MySQL.xml
cd ..
Use %cd% to get the current directory (i.e. the one that the batch file lives in)
e.g.
set JAVA_HOME=%cd%\jdk1.x.x
set PATH=%JAVA_HOME%\bin;%PATH%
set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%cd%\lib\myjar.jar;etc,etc
Related
I am using Windows CMD, and for some reason, I am getting this error message relating to my .java files. For example, I am typing:
javac FirstProgram.java
However, this error message occurs:
javac: file not found: FirstProgram.java
Usage: javac <options> <source files>
use -help for a list of possible options
I typed in javac -version, and I am currently using javac 1.8.0_144. Someone on in another Stack Overflow question suggested to change the System Variables. I used JAVA_HOME as the variable name, and I copied the path to my JDK folder, but thus far, I haven't had much luck. I still receive the same error message.
There are several problems here ... including why
PROBLEM 1:
"...I am not even allowed to save my FirstProgram.java in the Java
folder in Documents." <= ?!?
PROBLEM 2:
"...I got the error message about a false flag" <= This is probably a space " " in the path name
STRONG SUGGESTION:
Download Eclipse and try compiling and running your program from Eclipse. In other words, using an IDE, instead of the command line.
You can download Eclipse here:
http://eclipse.org
There is a good "starters tutorial" here:
Creating your first Java project
Run the dir or dir/p command to see your directory contents on your command prompt
c:\path\to\your program directory\dir
See if FirstProgram.java is listed or not? If not then you are in a wrong directory.
Now you have two options
You change to the correct directory using cd command or
You use the absolute path for your FirstProgram.java file
Navigate to that specific folder containing that program then [ Shift + L_Click ], click open cmd, then run it again to ensure it is being ran in that folder.
You should try to use absolute path while using the command if it says the file isn't found.
javac /some/directory/path/to/the/file/FirstProgram.java
Note: On command line, most of the shells would anyway not let you complete the path if the file doesn't exist there. And the other way if you're copying the path from an explorer/finder, it shall be guaranteed to be existing.
Edit: The absolute path as pointed out in comments would be using forward slashes in Windows, e.g :
javac \some\directory\path\to\the\file\FirstProgram.java
This is my a simplified DOSJavaIDE environment for simple test applications if Eclipse is too much of a hassle. I can point compiling and running an application to a specific JVM version. Study this script to see how folder structure and paths are given in each of the commands.
Folders and files
c:\projects\test1\classes\
c:\projects\test1\lib\
c:\projects\test1\lib\somelib1.jar
c:\projects\test1\lib\somelib2.jar
c:\projects\test1\src\
c:\projects\test1\src\test\GameLoop2.java
c:\projects\test1\src\META-INF\MANIFEST.MF
c:\projects\test1\javaenv.bat
javaenv.bat
#REM Standalone JavaDosEnvironment
#set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
#"%JAVA_HOME%\bin\java" -version
#set cmd=%1
#if "%cmd%"=="" (
#echo Please specify command to run ^(1..n or empty to exit^)
#echo 1=Compile, 2=Jar, 12=CompileJar
#echo 3=Run-test1 GameLoop2 with vsync
#SET /p cmd="1..n: "
)
#IF /I "%cmd%"=="1" set cmd=compile
#IF /I "%cmd%"=="compile" call :COMPILE
#IF /I "%cmd%"=="2" set cmd=jar
#IF /I "%cmd%"=="jar" call :JAR
#IF /I "%cmd%"=="12" set cmd=compilejar
#IF /I "%cmd%"=="compilejar" (
call :COMPILE
call :JAR
)
#IF /I "%cmd%"=="3" set cmd=run-test1
#IF /I "%cmd%"=="run-test1" call :RUN-test1
#goto :END
:COMPILE
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
set cp=./lib/somelib1.jar;./lib/somelib2.jar
"%JAVA_HOME%\bin\javac" -classpath "%cp%" -sourcepath ./src -d ./classes ./src/test/*.java
#goto :eof
:JAR
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
SET MF=./classes/META-INF/MANIFEST.MF
"%JAVA_HOME%\bin\jar" cvfm ./lib/test.jar %MF% -C ./classes .
#goto :eof
:RUN-test1
"%JAVA_HOME%\bin\java" -cp "./lib/*" test.GameLoop2 "fullscreen=false" fps=60 vsync=true
#goto :eof
:END
#pause
MANIFEST.MF
Implementation-Title: testapp
Implementation-Version: 1.0.0 (2017-07-21)
Implementation-Vendor: myname
Implementation-URL: http://my.homepage.com/
Run this script in a command-line such as javaenv.bat compile, javaenv.bat jar, javaenv.bat run-test1 or run without arguments to prompt for selection list.
See a customized manifest where you may write anything you want and is included in a ./lib/test.jar file. Compile target has few 3rd party dependency libraries in a classpath.
There's this BASH file:
#!/bin/bash
set -eu
DIR=`dirname $0`
JAR=$DIR/myjar.jar
CLASSPATH=$JAR:./
HEAP_SIZE=-size
java $HEAP_SIZE -cp $CLASSPATH something.something2 "$#"
That I want to turn into a .bat file to run on windows
This is what I have so far:
#ECHO OFF
set DIR = %cd%
set JAR = DIR/myjar.jar
set CLASSPATH = %JAR%:./
set HEAP_SIZE = -size
java %HEAP_SIZE% -cp %CLASSPATH% something.something2
How would I complete it to have the same behavior as the bash file?
%CD% can work, but your bash script seems to be setting the directory based off the path of the script (i.e., argument in $0) and not the current directory. To use the same directory as the batch file, you use %~dp0.
You missed % around your DIR variable when you wanted to expand the value.
You shouldn't have spaces around equal signs in your set statements.
The equivalent of $# in batch is %*.
You should quote your %CLASSPATH%. On Windows you are much more likely to encounter paths with spaces in them.
#ECHO OFF
set "DIR=%~dp0"
set "JAR=%DIR%/myjar.jar"
set "CLASSPATH=%JAR%:./"
set "HEAP_SIZE=-size"
java %HEAP_SIZE% -cp "%CLASSPATH%" something.something2 %*
I am not familiar with batch script, but I want to create a Windows Batch File to start a Java program. The problem is that it has to specific the path where JRE is installed. When you install both JRE7 and JRE8, the name of that JRE8 folder would call something like jre1.8.0_20 or jre1.8.0_40 with the version number in the back. When you have only JRE8 installed, the folder would call jre8. Is there an easier way to find where the most updated JRE installed and then execute it? Thanks.
start ..\..\Java\jre7\bin\javaw.exe -Xms512M -Xmx1024M -Djna.library.path=.\lib -cp example.jar; com.example.main
You should be able to get the location of javaw.exe by executing where java. This can be set as a variable inside a batch file like this:
# sets a variable called 'java' to the location of javaw.exe
for /f "delims=" %a in ('where javaw') do #set java=%a
# execute you jar file
%java% -jar <app.jar>
Noticed that the above only seems to work when running directly from the command line. Here is another example that should work in a batch file:
# run.bat
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('where javaw') do (
set java=%%a
)
!java! -jar %1
The above batch file should be called with the name of the jar file:
run.bat app.jar
I think it's best to just user JAVA_HOME and/or JRE_HOME and let the user / sysadmin worry what's installed.
I'm trying to find the location where JAVA_HOME env variale is set up and I tried to find in ~/.bash_profile , ~/.bashrc and /etc/profile but could not find the env variable.
But when I run echo $JAVA_HOME its gives out the value /usr/local/java.
Where else would the JAVA_HOME env variable.
BTW its a Red Hat Linux Server.
Try the file /etc/bash.bashrc, sometimes it is also used to initialise bash. If not, then try to find the word JAVA_HOME inside the files in /etc with grep -r BASH_HOME /etc
UPDATE
From man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes
commands from the first one that exists and is readable.
and also:
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.
So the only file that you haven't had a look at is ~/bash_login.
Probably JAVA_HOME is not in any of these files and what happens is that one of these files call another script, in that case, you'll have to read line by line if one of these scripts is "loading" more scripts from other places
What about:
grep JAVA_HOME /etc/*
and:
grep JAVA_HOME ~/.*
Please also check variables been sourced in all startup scripts in /etc/ and also under home directory,
it will look like . path/filename or source path/filename
EX: . /etc/var
or
source /etc/var
There are few standard locations which environment variables can be set.
/etc/profile
shell scripts under /etc/profile.d
/etc/bashrc
~/.bashrc
~/.bash_profile
Recommended way is to put environment variables in a script under /etc/profile.d. You can grep this directory, if other locations failed.
grep -R "JAVA_HOME" /etc/profile.d/
This post can give more details on the topic.
I am using the following batch script to run a Java Command Line tool.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R ./libs %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
java -cp !CLASSPATH! com.example.CLIApplication %*
popd
I have added the tool's directory to the System Variables PATH so that I can run it from any directory via the command prompt. This is working but the problem I am seeing is:
The tool's dir is C:\tool\
The user is in C:\
After executing the batch file the user is left in C:\tool\ not C:\
popd is getting called but the console navigates back to C:\too\ instead of staying in C:\
How do I ensure they users directory is not changed after the script finishes?
The setlocal without endlocal causes this problem here.
You only need to add an endlocal just before calling popd.
In your code the popd returns to your first directory, but as setlocal stores all variables and all open setlocals are closed by implicit endlocals when the batch is exited, it will also restore the cd variable.
I may be off track, but why aren't you using:
pushd .