This question already has answers here:
call java class in batch file
(6 answers)
Closed 9 years ago.
I need to run my Java Application through .bat file.
Can anybody help please.
Simply create a .bat file with the following lines in it:
#ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar
%JAVA_HOME%\bin\java -Xms128m -Xmx384m -Xnoclassgc ro.my.class.MyClass
Sure, call the java executable.
Mine is C:\Program Files\Java\jre6\bin\java.exe, so to run it I would do
C:\Program Files\Java\jre6\bin\java.exe -jar myjarfile.jar
If You have jar file then create bat file with:
java -jar NameOfJar.jar
It's the same way you run it from command line. Just put that "command line" into a ".bat" file.
So, if you use java -cp .;foo.jar Bar, put that into a .bat file as
#echo off
java -cp .;foo.jar Bar
#echo off
echo You Are going to creata Java Class
set /p Name=Enter your Class Name?:
echo Your class Name is %Name% & pause
echo To creat a Notepad
pause
notepad %Name%.java
set path=%PATH%;C:\Program Files\Java\jdk1.6.0_14\bin
pause
javac
echo Your java Path succsussfully set.
javac %Name%.java
pause
echo Successfully Compiled
java %Name%
pause
1)open a notpad
2)copy and past this code and save this file as
ex: test.bat
3)Double Click tha batch file.
4)put your java codes into the notepad and save it as
N.B.:- save this java file same folder that your batch file exists.
javac Application.java
java Application
pause
The javac command will compile the java program and the java command will run the program and pause will pause the result until you cross it.
Call the class which has main() method.
java MyClass
Here MyClass will have public static void main() method.
javac (.exe on Windows) binary path must be added into global PATH
env. variable.
javac MyProgram.java
or with java (.exe on Windows)
java MyProgram.jar
Related
I am running a java program using a .bat file. It works well after double clicking direcctly on the .bat, that's not the problem.
What I want now, is to run that .bt file (and the java program by extends) from the cmd at first, and then to be able to compile it from any other machine.
I have followed at first the following answer : How do I run a java program from a different directory? , but it didn't work for me.
Here is my .bat file :
#echo on
set CLASSPATH=%CLASSPATH%;.;lib/console.jar;lib/log4j-1.2.13.jar;lib/prog1.jar;lib/prog1_newOption.jar;lib/org.hamcrest.core_1.3.0.v201303031735.jar;lib/RXTXcomm.jar;lib/trace.jar;lib/xercesImpl.jar;lib/xml-apis.jar
java -cp "$/prog1_newOption/src/main/Main" %UsersCommand%
pause
exit
%cmd%
Maybe have I to look for the main path in the .bat file and then parse it the the "java command programm" line?
Thank you so much for your help
I store jar files in C:\Users\myuser\javatools\avro-tools
And added them to my PATH:
echo %PATH%
...;
C:\Users\myuser\javatools\avro-tools;
I can run them by specifying the full path:
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar
But cannot run them without the full path:
java -jar avro-tools-1.8.1.jar
Error: Unable to access jarfile avro-tools-1.8.1.jar
I need to run jar files without changing to the directory, nor specifying these full paths.
UPDATE: Added %*
I'd recommend creating a batch file and run that instead.
avro-tools-1.8.1.bat
#echo off
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Place .bat file somewhere in PATH, and run by simply typing:
avro-tools-1.8.1.bat -abc def
The %* in the .bat file gets replaced with any argument passed to the .bat file, so the -abc def arguments are passed to the avro-tools program in the args array to the main method.
If you have multiple versions of Java installed, you can then choose which one to use when running that .jar file, by also qualifying the java command.
avro-tools-1.8.1.bat
#echo off
"C:\Program Files\Java\jdk1.8.0_181\java.exe" -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Now that code will run with Java 8, even if Java 8 is not the default Java on your machine.
Everytime I want to build and run my program I do:
javac myProgram.java
java myProgram
I want to do something like this:
buildrun = javac (some_argument).java && java (some_argument)
so after I can just
buildrun myProgram
How to achieve this on Windows?
As other's have suggested you can simply create a batch file to build and run your program. Copy this in Notepad and save as .bat.
#echo off
set /p class="Enter Class: "
javac "%class%".java
java "%class%"
As you want, the batch file will ask for a FileName when it runs. In your case, you can set it to 'myProgram' and it will then compile and run the program.
Just make sure your batch file and your java file reside in the same folder for this script to run. You can always tweak the bat file to even accept the full path of the Java file.
To compile and run Java with single command line in cmd use:
java myProgram.java
You can use Makefile this way:
Create a file named Makefile within the same folder where your java files reside.
Add these contents to Makefile
run: compile
java $(class)
compile:
javac $(class).java
From terminal, run this command:
make run class=myProgram
This way, it will compile first then run your java class in a single command
Yet another solution. Create buildrun.cmd file with the following code:
#echo off
javac %1.java
if errorlevel = 0 goto RUN
:ERROR
echo "Build fails!"
goto END
:RUN
java %1
:END
Now you can pass the class name which should be processed. For example: buildrun MyProgram to compile MyProgram.java and run MyProgram.class
In this case execution will performs only if your class was compiled successful.
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 $*
this is the command.
java -cp clojure.jar;sum.jar CalculateSum
sum.jar is a jar file made from clojure and java code.
CalculateSum is file which contains main method of java.
error from cygwin
can't execute binary file, Error 126
Cygwin provides you with a *nix environment within Windows, so that you might have to change the classpath separator to colons:
java -cp clojure.jar:sum.jar CalculateSum
Try:
java -cp clojure.jar:sum.jar:. CalculateSum
If you execute in the place you have the root of packages for CalculateSum.class
java -cp "clojure.jar;sum.jar" CalculateSum this is working