java和bat相互调用

1.bat 启动java main函数

1.将project导出JAR,过程中注意选择main class

2.新建bat,设置环境变量

3.1启动程序,如果想在黑窗口中打印数字,可以system.out,程序运行万

@echo off
@setlocal

set JAVA_HOME=jdk1.6.0_25
set PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin

java -jar start.jar
或者直接调用class文件

@echo off
@setlocal

set JAVA_HOME=..\jdk1.6.0_25
set PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin

java usb/CreateStartBat


2.java调用bat

String cmd = "cmd /c start  " +
				desDisk +
				":\\server\\start.bat";
		Runtime rt = Runtime.getRuntime();
		try {
			rt.exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
3.若有需要传参数可以新建文件“xxx.bat”

	public static void CreateBat(){

		String desDisk = DiskUtil.getDesDisk();
		String diskid = System.getProperty("user.dir").split(":")[0];//程序所在盘
		System.out.println(desDisk+diskid);
		
		File file = new File(desDisk+"://"+COPY_FOLDER+"//"+FILE_NAME);
		 FileWriter fw = null;  
		try {
			File file2 = new File(desDisk+"://"+COPY_FOLDER);
			if(!file2.exists()){
				file2.mkdir();
			}
			
			file.createNewFile();
			fw = new FileWriter(file);  
			fw.write("@echo off\r\n"); 
			fw.write("@setlocal\r\n");  
			fw.write("set JAVA_HOME=" +	diskid +":jdk1.6.0_25\r\n");  
			fw.write(" set PATH=%JAVA_HOME%\\bin;%JAVA_HOME%\\jre\\bin \r\n");  
			fw.write("echo %~d0\r\n");  
			fw.write("%~d0\r\n");  
			fw.write("cd %~dp0\r\n");  
			fw.write("set JAVA_OPTIONS=%JAVA_OPTIONS% -server\r\n");  
			fw.write("set JAVA_OPTIONS=%JAVA_OPTIONS% -Xmx512m\r\n");  
			fw.write("set JAVA_OPTIONS=%JAVA_OPTIONS% -XX:MaxPermSize=128m\r\n");  
			fw.write("set JAVA_OPTIONS=%JAVA_OPTIONS% -Djava.io.tmpdir=tmp\r\n");  
			fw.write("set JAVA_OPTIONS=%JAVA_OPTIONS% -Dderby.system.home=derby\r\n");  
			fw.write("set OSGI_OPTIONS=%OSGI_OPTIONS% -Dosgi.configuration.area=osgi-config\r\n");  
			fw.write("set OSGI_OPTIONS=%OSGI_OPTIONS% -Dosgi.noShutdown=true\r\n");  
			fw.write("set OSGI_OPTIONS=%OSGI_OPTIONS% -Dosgi.install.area=./\r\n");  
			fw.write("echo 启动时JAVA配置: %JAVA_OPTIONS%\r\n");  
			fw.write("echo 启动时OSGI配置: %OSGI_OPTIONS%\r\n");  
			fw.write("echo 启动参数: %1 %2 %3 %4\r\n");  
			fw.write("java %JAVA_OPTIONS% %OSGI_OPTIONS% -jar core/org.eclipse.osgi_3.3.1.R33x_v20070828.jar -clean -consoleLog %1 %2 %3 %4");  
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {   

            try {   

                fw.close();   


            } catch (Exception e) {   

                e.printStackTrace();   

            }   

        }   

	
	}



你可能感兴趣的:(java,bat)