Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java commands in a batch file... 1

Status
Not open for further replies.

jhampton13

Programmer
Dec 19, 2003
5
GB
I am calling a java command in a batch file. I can call the first command fine, but the .bat file hangs before calling the second command. It is basically a file that sends a test message to a phone if something is wrong. It will send the message to the first phone fine, but not the second one. I dont know if i need to have something between the two for it to run ok or not. I have heard the & between 2 java calls, but it is not working. I would appreciate any advice anyone has.. Thank you..
 
Do both java calls execute on their own (ie just on the command line, not in a batch file) ?
 
They will both execute on the command line. But only the first will execute in the batch file. The program freezes before the second command will execute. I was thinking maybe putting them in a loop of some kind.. I could be way off...
 
Strange, I can execute two calls no problem from a batch file ...
Code:
batch file :
-------------------------------
@echo off
set CLASSPATH=%CLASSPATH%;C:\java
java FileStuff C:\test.pdf C:\new.pdf
echo Done first call
java FileStuff C:\test.pdf C:\new1.pdf
echo Done second call


-----------------------------------
test java code :
-----------------------------------
import java.io.*;
public class FileStuff {
	public boolean copy(String from, String to) {
		File f = new File(from);
		File t = new File(to);
		try {
			byte[] bytes = new byte[(int)f.length()];
			FileInputStream fis = new FileInputStream(f);
			fis.read(bytes);
			FileOutputStream fos = new FileOutputStream(t);
			fos.write(bytes);
			fos.flush();
			fos.close();
			fis.close();

			if (f.length() == t.length()) {
				return true;
			} else {
				return false;
			}
		} catch (IOException ioe) {
			ioe.printStackTrace(System.err);
			return false;
		}
	}

	public static void main(String args[]) {
		new FileStuff().copy(args[0], args[1]);
	}

}


This executes no problem.

I guess one daft/lazy way of getting around the problem would be to call a "driver" program, which will run your java for you - maybe something like :

Code:
public class DumbDriver {
	public static void main(String args[]) throws java.io.IOException {
		for (int i = 0; i < args.length; i++) {
			Runtime.getRuntime().exec(args[i]);
		}
	}

}

@echo off
set CLASSPATH=%CLASSPATH%;C:\java                                           
java DumbDriver &quot;java FileStuff C:\test.pdf C:\new.pdf&quot; &quot;java FileStuff C:\test.pdf C:\new1.pdf&quot;

or maybe use the java.lang.ClassLoader to dynamically load classes and run methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top