Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


batch files make life easy

How do I keep from typing in many different lines of Java at a command-line prompt? by carpeliam
Posted: 28 Jun 00

Often times, if I'm working with Sun's JDK as my compiler, I'm at the DOS prompt typing in the same commands over and over again. It's a pain; really, it is. If I need to compile and run, that's two lines each time (javac and java), and if I have to edit a classpath, that's another pain in the neck. It's just not fun, and we all know that programming (especially Java programming) is fun, and should stay that way all the time.

Well, here's what I do- I create a batch script that will run my Java for me. When sometimes I might need to type 2 or 3 lines at a DOS prompt to run my program, now I just run a batch file that runs everything for me.

Here's how I do it:

1) I create a text file- let's say "new.txt".
2) Now I have to rename it to "new.bat".
3) Then I list the commands I'd throw at a command line.

@set CLASSPATH="..\..\..\class;..\..\..\src;..\..\..\lib\Stuff.jar;..\..\..\lib\Tools.jar;"
@set DESTINATION="..\..\..\..\..\..\..\class"

@javac -classpath %CLASSPATH% -d %DESTINATION% MyJavaClass.java
@java -classpath %CLASSPATH% MyJavaClass

4) Then I run the batch file.

I'll explain this process. First, if you edit the classpath in a batchfile, you don't need to reboot your machine (as you would if you typed it in autoexec.bat) and you don't have to worry about your classpath interfering with the permanent classpath already set in autoexec.bat.
Second, you don't have to type it all in every time if you put it in a batch file- just type it once and run it anytime (as opposed to writing it once and running it anywhere, uh huh).

Now I'll explain what exactly is in the batch file. First, the @ symbols. Those are there so the statements don't get echoed at the commandline- I don't want to actually see the statements, I just want them executed. Second, the "set CLASSPATH=....". This is the relative path to all the classes that need to be included when I run my program. If you don't remember old DOS stuff, "..\" means the parent directory- so if my class files are under "C:\class" and my batch file is in "C:\myjava\source\project1\", my classpath would have to include "..\..\..\class" (I think that's right). The .jar files are zipped up files that contain class files inside them. You may or may not have to worry about them, depending on whether or not you're working with .jar files.

DESTINATION is a variable; the "-d" switch on the compiler specifies the output directory where my class file will go. If you want your class files to stay in the same directory, then you don't need this.

Then my javac line. This line is included if I'm still testing my file (I can leave it out if the file's already compiled, and the class that's available to me is the compiled version that I want to use). The "-classpath" switch will tell me where to find my other class files that I might need for building. Again, the "-d" switch is the output directory.

Then, I have my java line. "-classpath" is the classpath switch, just like in javac; %CLASSPATH% is the classpath we just specified (the % on both sides means it's a variable in the batch file that you have just set).

I hope that helps some people out!

Liam Morley

Back to Java FAQ Index
Back to Java Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close