Creating a .bat file to compile .java files

Hi,

I am trying to create a .bat file to compile java files. I have created the .bat in the same folder as teh .java file I am trying to compile. I know the path is set to the proper folder to find the compiler, but I keep getting an error saying the system can not find the specified file, or can not read fileName.java. Everything is spelled correctly, and I know it works because it will compile from the command prompt. Any suggestions?

[453 byte] By [kawjr2121a] at [2007-10-3 4:29:47]
# 1
I'm guessing your bat looks something likejavac %1(usage: mybat myfile.java)or maybejavac %1.java(usage: mybat myfile)Note no need for .java extension.Where this will break down is if your filename has spaces. If it does, change it.
C_Heeneya at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 2

Does it matter that is is in a briefcase? I have tried the following:

javac -sourcepath=<"C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2"> Facts.java

...and the following:

javac Facts.java

I tried the %1, it looks as if it compiled, but no .class file showed up.

kawjr2121a at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 3

How exactly does the bat file look like?

How do you start it?

Here is an example that works

I have a file called Test.java and one called test.bat.

Both are in the folder c:\java

test.bat looks like this:

javac Test.java

Then I can type test<enter> at the command prompt and it will look like this

C:\java>test

C:\java>javac Test.java

C:\java>

Zoidberg42a at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 4

rather than including a sourcepath, just specify the full location. Using your example, this would read like

javac "C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2\Facts.java"

Note that everything is inside the inverted commas " "

I tried this on a hello world Java program inside a brief case and it works fine.

The class file is created inside the briefcase.

If you want to create a bat file to do this from anywhere, modify your batch file to look like this:

c:\

cd "C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2"

javac %1.java

java %1

Again, you would use this as follows

mybat Facts

Don't forget to leave out the .java extension.

This will compile the java file and attempt to run it for you.

If you don't want it to run, leave out the java%1

If you create other java files in this same briefcase folder you can use mybat to compile them too.

For example

mybat Facts2

C_Heeneya at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 5

Thanks, that worked for compiling the .java file & it did create the .class file in the briefcase, but I have a seperate bat file for running the .class file. It does not seem to work, it says no class definition found. Now I am new to bat files, what exactly does the %1 do? I have also tried to compile and run in the same bat, heres what it looks like, this is when the no class def error showed up, it compiles fine, and the class file shows up, but I get the error:

javac "C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2\Facts.java"

java "C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2\Facts"

Thanks

kawjr2121a at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 6

%1 is the first argument of the command line, %2 is the second and so on.

If I have a bat file called mybat and I pass it 2 arguments like this

mybat foo bar

the system will interpret foo as the first argument and bar as the second.

It just gives you a nice way of using the same bat file for something else later on for example

mybat foo ton

and so on.

So, with regard to the error, fire up a command prompt.

type in

cd "C:\Documents and Settings\Kevin\Desktop\School Stuff\AP Comp Sci\PowerPoints\Chapter 02\Executables\Section 2.2"

java Facts

If it doesn't run, it's because your Classpath isn't pointing to the correct locations for other classes you have imported into your Facts program.

If that is the case, satisfy yourself that a simple HelloWorld application runs from your briefcase.

Again, you should be able to reuse your mybat file for that purpose

C_Heeneya at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 7
C_Heeney,You have been extremely helpful. I finally got it running. I am not sure how to award the dollars i have for this topic, but I will send them to you. Just let me know how to do it.kawjr2121
kawjr2121a at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...
# 8
K,Glad to have been able to help.Goto http://developers.sun.com/forums/dukedollars.html and read the bit about awarding Duke Dollars.Cheers
C_Heeneya at 2007-7-14 22:32:57 > top of Java-index,Developer Tools,Java Compiler...