I want to run my java program through a .bat file. My java program is present in a bin subfolder of the java folder. My batch file is present in the batch folder.
我想通过.bat文件运行我的java程序。我的java程序存在于java文件夹的bin子文件夹中。我的批处理文件存在于批处理文件夹中。
cd..
cd java
cd bin
javac screenshot.java
java screenshot
pause
The above is the script in my batch file.The program is getting compiled, but while running i am getting:
以上是我的批处理文件中的脚本。程序正在编译,但在运行时我得到:
Error:Could not find or load main class screenshot.
Can someone tell me where i went wrong? I am stuck.
谁能告诉我哪里出错了?我被卡住了。
4 个解决方案
#1
0
You need to set your classpath prior to running the Java program. Also, it makes sense to combine the cd
commands into one:
您需要在运行Java程序之前设置类路径。此外,将cd命令合并为一个是有意义的:
cd ..\java\bin
javac screenshot.java
set CLASSPATH=.
java screenshot
pause
#2
0
As inquizitive pointed out, I made a mistake with the name. The class name is Screenshot, not screenshot. I am so sorry for troubling you guys.Thank You
正如inquizitive指出的那样,我的名字出错。类名是截图,而不是截图。我很抱歉让你们烦恼。谢谢
#3
0
I had a similar problem. Maybe that solution will help some people for faster compile their java-programs.
我有类似的问题。也许这个解决方案可以帮助一些人更快地编译他们的java程序。
It would be easier if you would make the batch reusable.
如果您可以使批次可重复使用会更容易。
@echo off
title Java Compiler
:start
cd C:\[...]
set /p id="Enter Name without file extension:"
javac %id%.java
set /p ic="Enter Name without file extension: "
java %ic%
set /p ib="Want to exit? (type in "exit", else nothing): "
%ib%
set /p ia="Want to Clear the Window? (type in "cls", else nothing): "
%ia%
goto start
Line 3 put your project-folder in there, for example "C:\Users\User\Desktop\Projects"
第3行将项目文件夹放在那里,例如“C:\ Users \ User \ Desktop \ Projects”
Line 4 is a new variable, asks you for your source-file. (For the source-file "example.java" type in "example")
第4行是一个新变量,要求您提供源文件。 (对于“example”中的源文件“example.java”类型)
Line 5 compile the source file.
第5行编译源文件。
Line 6 is another variable, asks for the compiled file. (For the file "example.class" type in "example")
第6行是另一个变量,要求编译文件。 (对于“example.class”文件,在“example”中输入)
Line 7 run your compiled java-program.
第7行运行已编译的java程序。
#4
-1
It can be done in two ways:
它可以通过两种方式完成:
a) Set JAVA_PATH is in environmental variables.(Right Click->My Computer->Advanced) In .bat file please add the following:
java screenshot pause
a)设置JAVA_PATH是环境变量。(右键单击 - >我的电脑 - >高级)在.bat文件中请添加以下内容:java screenshot pause
b) In .bat file do the following entries: a) set PATH="%PATH%;%YOUR JAVA PATH TILL BIN% b) java screenshot
b)在.bat文件中执行以下输入:a)设置PATH =“%PATH%;%YOUR JAVA PATH TILL BIN%b)java screenshot
The above two approaches should work. If you are setting environment variable please make sure to open a new window.
以上两种方法应该有效。如果您要设置环境变量,请务必打开一个新窗口。
#1
0
You need to set your classpath prior to running the Java program. Also, it makes sense to combine the cd
commands into one:
您需要在运行Java程序之前设置类路径。此外,将cd命令合并为一个是有意义的:
cd ..\java\bin
javac screenshot.java
set CLASSPATH=.
java screenshot
pause
#2
0
As inquizitive pointed out, I made a mistake with the name. The class name is Screenshot, not screenshot. I am so sorry for troubling you guys.Thank You
正如inquizitive指出的那样,我的名字出错。类名是截图,而不是截图。我很抱歉让你们烦恼。谢谢
#3
0
I had a similar problem. Maybe that solution will help some people for faster compile their java-programs.
我有类似的问题。也许这个解决方案可以帮助一些人更快地编译他们的java程序。
It would be easier if you would make the batch reusable.
如果您可以使批次可重复使用会更容易。
@echo off
title Java Compiler
:start
cd C:\[...]
set /p id="Enter Name without file extension:"
javac %id%.java
set /p ic="Enter Name without file extension: "
java %ic%
set /p ib="Want to exit? (type in "exit", else nothing): "
%ib%
set /p ia="Want to Clear the Window? (type in "cls", else nothing): "
%ia%
goto start
Line 3 put your project-folder in there, for example "C:\Users\User\Desktop\Projects"
第3行将项目文件夹放在那里,例如“C:\ Users \ User \ Desktop \ Projects”
Line 4 is a new variable, asks you for your source-file. (For the source-file "example.java" type in "example")
第4行是一个新变量,要求您提供源文件。 (对于“example”中的源文件“example.java”类型)
Line 5 compile the source file.
第5行编译源文件。
Line 6 is another variable, asks for the compiled file. (For the file "example.class" type in "example")
第6行是另一个变量,要求编译文件。 (对于“example.class”文件,在“example”中输入)
Line 7 run your compiled java-program.
第7行运行已编译的java程序。
#4
-1
It can be done in two ways:
它可以通过两种方式完成:
a) Set JAVA_PATH is in environmental variables.(Right Click->My Computer->Advanced) In .bat file please add the following:
java screenshot pause
a)设置JAVA_PATH是环境变量。(右键单击 - >我的电脑 - >高级)在.bat文件中请添加以下内容:java screenshot pause
b) In .bat file do the following entries: a) set PATH="%PATH%;%YOUR JAVA PATH TILL BIN% b) java screenshot
b)在.bat文件中执行以下输入:a)设置PATH =“%PATH%;%YOUR JAVA PATH TILL BIN%b)java screenshot
The above two approaches should work. If you are setting environment variable please make sure to open a new window.
以上两种方法应该有效。如果您要设置环境变量,请务必打开一个新窗口。