java 如何使用批处理文件运行java项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16462462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:02:04  来源:igfitidea点击:

how to run java project using batch file

javabatch-file

提问by genky

I have written java project which contains only one class.

我编写了只包含一个类的 java 项目。

I want to run it on another pc's using batch file.

我想使用批处理文件在另一台电脑上运行它。

Please advise me how to do this. My class contains parameters for running.

请告诉我如何做到这一点。我的课程包含运行参数。

I organize them in such way:

我以这种方式组织它们:

Scanner in = new Scanner(System.in);
System.out.println("Enter value1: ");
value1 = Double.parseDouble(in.nextLine()); 

Path to my java is C:\Program Files\Java\jdk1.7.0_09\binMy project is in eclipse workspace.

我的 java 的路径是C:\Program Files\Java\jdk1.7.0_09\bin我的项目在 eclipse 工作区中。

I need to add than I want to see in cmd output "Enter value1: " and have possibility to set this value during execution.

我需要添加比我想在 cmd 输出中看到的“输入值1:”,并且有可能在执行过程中设置这个值。

回答by Juned Ahsan

You need to write a batch file as mentioned here:

您需要编写此处提到的批处理文件:

"%JAVA_HOME%\bin\java" -cp "path of your class files" nameofyourclass

回答by Horrendus

The steps to run a Java file are pretty easy:

运行 Java 文件的步骤非常简单:

-) javac NameOfClass.java

-) javac NameOfClass.java

-) java -cp . NameOfClass

-) java -cp 。班级名称

So the only thing you have to put in the Batch file are these 2 commands.

因此,您唯一需要放入批处理文件的是这 2 个命令。

回答by ErrorNotFoundException

You first need to compile your code into a Jar file:

您首先需要将代码编译成 Jar 文件:

Then go to Notepad and Type

然后转到记事本并键入

@echo off
java -jar Myjarfile.jar parameter

Save the file as anyfilename.bat in the same location as the Jar File.

在与 Jar 文件相同的位置将文件另存为 anyfilename.bat。

Open CMD and Navigate to the Location of your .bat and .jar

打开 CMD 并导航到 .bat 和 .jar 的位置

execute like this:

像这样执行:

mybatfile.bat

回答by user2270629

if you use Eclipse (or Idea IntelliJ), you can see the command line used by your IDE in order to make your program run.

如果您使用 Eclipse(或 Idea IntelliJ),您可以看到 IDE 使用的命令行,以使您的程序运行。

For instance, this is my command line. After my "very-long" classpath, you can see the name of the class you want to run ("Main" for me).

例如,这是我的命令行。在我“很长”的类路径之后,您可以看到要运行的类的名称(对我来说是“Main”)。

"C:\Program Files\Java\jdk1.6.0_22\bin\java" 
   -Didea.launcher.port=7534
  "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 12.0.4\bin" -Dfile.encoding=UTF-8
  -classpath "C:\Pro[...snip...]12.0.4\lib\idea_rt.jar" 
  com.intellij.rt.execution.application.AppMain Main

Let me know if it helps.

如果有帮助,请告诉我。

回答by Ngwe

cd "C:\Program Files\Java\jdk1.8.0_45\bin"
javac classname.java
java classname
pause

回答by Ravindra Bawane

Step-1:Create a Java Class and write a below code and save as Math.java in Notepad or using Eclipse IDE.

步骤 1:创建一个 Java 类并编写以下代码并在记事本中或使用 Eclipse IDE 保存为 Math.java。

public class Math 
{
public static void addition(){
int a = 20;
int b = 10;
System.out.println(a+b);
}
public static void subtraction(){
int a = 20;
int b = 10;
System.out.println(a-b);
}
public static void division(){          
int a = 20;
int b = 10;     
System.out.println(a/b);
}   
public static void multiplication(){        
int a = 20;
int b = 10;
System.out.println(a*b);
}   
public static void main( String[] args )
{
System.out.println( "Lets Do the Math " );         
Math objectMath = new Math();       
objectMath.addition();
objectMath.subtraction();
objectMath.division();
objectMath.multiplication();      
}
}

Step-2:Convert the file to executable jar file using Eclipse save as Math.Jar

步骤 2:使用 Eclipse 将文件转换为可执行的 jar 文件,另存为 Math.Jar

Step-3:Create a bat file in Notepad Or Notepad++ and save as Math.bat

步骤 3:在 Notepad 或 Notepad++ 中创建一个 bat 文件并另存为 Math.bat

@echo off
"%JAVA_HOME%\bin\java" -cp "Path of XYZ.Java File" XYZ
java -jar Math.jar parameter
echo Successfully Compiled
pause

Step-4:Double click the .bat file it should work.

第 4 步:双击它应该可以工作的 .bat 文件。

Note: Make sure that bat file java file and jar file all in same when you try.

注意:尝试时请确保 bat 文件 java 文件和 jar 文件都相同。

OutputLets Do the Math
30
10
2
200
Successfully Compiled
Press any key to continue . . .

输出Lets Do the Math
30
10
2
200
成功编译
按任意键继续。. .