[英]execute jar file in java program
I want to create an java program to compress an css file using YUI I am new learner in java. 我想创建一个java程序来使用YUI压缩css文件我是java中的新学习者。
My Code is: 我的代码是:
import java.io.BufferedInputStream;
import java.io.IOException;
public class Run extends Object
{
public static void main(String args[]) throws IOException, InterruptedException
{
System.out.println("Calling jar");
Process p = Runtime.getRuntime().exec("java -Xmx32m -jar yui.jar in.css");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
synchronized(p)
{
p.waitFor();
}
System.out.println(p.exitValue());
int b = 0;
while((b = bis.read()) > 0)
{
System.out.print((char)b);
}
System.out.println("Called jar");
}
}
I took reference from here . 我从这里参考了 。
the command: 命令:
java -Xmx32m -jar yui.jar in.css
works fine in cmd but I get no output when I run above program the output I get for above is: 在cmd中运行正常,但是当我运行上面的程序时,我没有得到输出,我得到的输出是:
Calling jar
1
Called jar
Please tell Me what I am doing wrong or what is the right way of doing this. 请告诉我我做错了什么或者这样做的正确方法是什么。
You are getting the "1" because there is nothing to read in the stream. 你得到“1”,因为在流中没有任何东西可读。
I'm guessing that you're trying to run the file from Eclipse or some other IDE. 我猜你正在尝试从Eclipse或其他IDE运行该文件。 If so, you need to place your
yui.jar
and in.css
files to same directory relative to where your Run
class is. 如果是这样,您需要将
yui.jar
和in.css
文件放在相对于Run
类所在目录的相同目录中。
If you're using default run configurations, you'll just want to put the files into the root directory of your eclipse project. 如果您使用的是默认运行配置,则只需将文件放入eclipse项目的根目录即可。 For example, this works for me:
例如,这对我有用:
Test
src
com
test
Run.java
yui.jar
in.css
A better way to handle your situation is to use absolute or relative paths instead of just specifiying yui.jar
or in.css
. 处理您的情况的更好方法是使用绝对路径或相对路径,而不是仅指定
yui.jar
或in.css
。 Create two variables for the two relative paths and then create the command string. 为两个相对路径创建两个变量,然后创建命令字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.