How do I execute a Java project using batch?
Moderator: DosItHelp
How do I execute a Java project using batch?
Hi everyone. This is my first post here.
I'm working on a Java game for a school project and I want users to be able to start it easily. I thought creating a batch file might be a good way to do this, but I have never actually written one before and only have a vague idea right now how the language/syntax works.
Ideally, I'm hoping to create a file (of any kind) that executes my code (in .java or .class files) when a user double clicks it. The Java IDE I'm using (JCreator) is very simplistic and doesn't provide the option to create a .exe file or an executable .jar file. Could I use batch to do the job? How?
Just to simplify things, the batch file is in the same directory as all of my code. My plan is to create a shortcut to it and place that on the desktop.
Thank you!
I'm working on a Java game for a school project and I want users to be able to start it easily. I thought creating a batch file might be a good way to do this, but I have never actually written one before and only have a vague idea right now how the language/syntax works.
Ideally, I'm hoping to create a file (of any kind) that executes my code (in .java or .class files) when a user double clicks it. The Java IDE I'm using (JCreator) is very simplistic and doesn't provide the option to create a .exe file or an executable .jar file. Could I use batch to do the job? How?
Just to simplify things, the batch file is in the same directory as all of my code. My plan is to create a shortcut to it and place that on the desktop.
Thank you!
Re: How do I execute a Java project using batch?
Well I think you can do it with built-in java? Don't know much of it.
But going through a quick reading in java's help (command line: java -?), maybe you can start it like any other program.
Untested:
By the way, I'm assuming "javaProgram" is a defined string variable for your program name. Replace it with the actual name if preferred; remember to enclose it in quotes if it contains spaces.
But going through a quick reading in java's help (command line: java -?), maybe you can start it like any other program.
Untested:
Code: Select all
@start java -cp "%javaProgram%"
By the way, I'm assuming "javaProgram" is a defined string variable for your program name. Replace it with the actual name if preferred; remember to enclose it in quotes if it contains spaces.
Re: How do I execute a Java project using batch?
Thanks for the help, but this didn't work: One command line popped up and called another, which closed without doing anything.
I tried entering "java -?" into command prompt like you did, but most of the information was meaningless to me. Should this code execute the java file, or would it just open it in the IDE?
I tried entering "java -?" into command prompt like you did, but most of the information was meaningless to me. Should this code execute the java file, or would it just open it in the IDE?
Re: How do I execute a Java project using batch?
Is this any better? I know little of java stuff too.
@echo off
java -jar "my jar file.jar"
@echo off
java -jar "my jar file.jar"
Re: How do I execute a Java project using batch?
NcAdams wrote:Thanks for the help, but this didn't work: One command line popped up and called another, which closed without doing anything.
I tried entering "java -?" into command prompt like you did, but most of the information was meaningless to me. Should this code execute the java file, or would it just open it in the IDE?
I'm a complete layman in Java, so I'm afraid I can't help anymore than I did (tried to do, actually).
Re: How do I execute a Java project using batch?
Running java -jar (referring to foxi's code) should work flawlessly if you convert your .java to .jar.
http://www.skylit.com/javamethods/faqs/createjar.html
skylit wrote:Creating a jar File in JCreator
You can configure a "tool" that will automate the jar creation process. You only need to do it once.
Click on Configure/Options.
Click on Tools in the left column.
Click New, and choose Create Jar file.
Click on the newly created entry Create Jar File in the left column under Tools.
Edit the middle line labeled Arguments: it should haveCode: Select all
cvfm $[PrjName].jar manifest.txt *.class
Click OK.
Now set up a project for your program, create a manifest file manifest.txt or copy and edit an existing one. Place manifest.txt in the same folder where the .class files go. Under View/Toolbars check the Tools toolbar. Click on the corresponding tool button or press Ctrl-1 (or Ctrl-n if this is the n-th tool) to run the Create Jar File tool.
With Windows Explorer, go to the jar file that you just created and double click on it to run.
Creating a jar File in Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:Code: Select all
C:\>cd \mywork
Set path to include JDK’s bin. For example:Code: Select all
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):Code: Select all
C:\mywork> javac *.java
Create a manifest file:Code: Select all
C:\mywork> echo Main-Class: DanceStudio >manifest.txt
Create a jar file:Code: Select all
C:\mywork> jar cvfm DanceStudio.jar manifest.txt *.class
Test your jar:Code: Select all
C:\mywork> DanceStudio.jar
http://www.skylit.com/javamethods/faqs/createjar.html
Re: How do I execute a Java project using batch?
Thanks so much!
The one thing I'm still having trouble with is the manifiest. Right now I have a file named manifest.txt: It's in my class folder, and it contains the text "Main-Class: Main" plus a new line. But for some reason when I try to create the jar file with JCreator, it always tells me the file manifest.txt does not exist. Then it creates a blank manifest file and the program won't run as a result.
I tried going into the jar file itself (using 7zip) and changing the manifest from there, but I got the same error: "Failed to load Main-Class attribute".
Do you know how I could fix this?
The one thing I'm still having trouble with is the manifiest. Right now I have a file named manifest.txt: It's in my class folder, and it contains the text "Main-Class: Main" plus a new line. But for some reason when I try to create the jar file with JCreator, it always tells me the file manifest.txt does not exist. Then it creates a blank manifest file and the program won't run as a result.
I tried going into the jar file itself (using 7zip) and changing the manifest from there, but I got the same error: "Failed to load Main-Class attribute".
Do you know how I could fix this?
Re: How do I execute a Java project using batch?
NcAdams wrote:The one thing I'm still having trouble with is the manifiest. Right now I have a file named manifest.txt: It's in my class folder, and it contains the text "Main-Class: Main" plus a new line
According to Fawers' example it looks like the text should contain the project name, maybe the jar name. In this example there is going to be a trailing space, maybe that doesn't matter and maybe it does.
echo Main-Class: DanceStudio >manifest.txt