Call a Java class in a batch file
Asked Answered
T

6

7

I want to call a Java class from a batch file.

How can I do that?

Totipalmate answered 4/11, 2010 at 7:2 Comment(1)
You might want to accept an answer or respond if none of the work...Funiculus
M
4
@ECHO OFF
java -jar "Path/To/The/Jar/Whatever.jar"

I would recommend first jaring up your class(es) and providing a link to the jar.

Millinery answered 11/8, 2013 at 18:41 Comment(0)
C
3

If you are having:

  • a class Myclass,
  • with a package name of com.mycomp.util.

Then:

  • use the parent dir of com e.g. c:\src
    (the folder that contains the com package),

  • store the below commands in a batch file:

    cd c:\\src  
    java -cp jar1;jar2; com.mycomp.util.Myclass
    

Execute the batch file to run the Java program.

Celestaceleste answered 4/11, 2010 at 7:28 Comment(0)
D
2

If you have compiled your .java file, and have the .class file, containing bytecode for your main function, then just run:

java myclass

where myclass is the module name (file has to be myclass.class).

Ducktail answered 4/11, 2010 at 7:6 Comment(1)
This won't work if myclass isn't in the default package. Also, by Java conventions, it should be "Myclass" instead of "myclass"Falsecard
S
1

Just use this in ur .bat file
java -classpath folderName/example.jar; com.example.package.ExampleProgram if you are placing the .bat file in the same folder with the jar, then its not necessary to mention the folderName

Southeaster answered 12/9, 2012 at 12:45 Comment(0)
J
1

You can do the following:

  1. Open a new Text File in Notepad.

  2. Write the following lines of code, then saves it as "MyFile.bat"
    (Note: Save it as a .BAT File)

    @ECHO OFF
    javac YourClass.java
    java YourClass

  3. Now double click the BAT file to execute your Java program.

Note: The BAT file and Java Class should be in the same directory.

Justify answered 25/10, 2019 at 10:17 Comment(0)
A
0
@echo off

java -jar "C:\path_to_jar_directory\test.jar"
"C:\path_to_arguments\property.properties"
Annorah answered 23/10, 2014 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.