我想从shell脚本运行一个Java程序.所以我做了类似下面的事情
我的测试Java文件如下
public class EchoTest {
public static void main (String args[]) {
System.out.println ("scuccess ..!!");
}
Run Code Online (Sandbox Code Playgroud)
我的测试shell脚本文件如下
out=$(java EchoTest)
echo $out
Run Code Online (Sandbox Code Playgroud)
我编译了java程序,然后我运行了那个shell脚本(比如$ sh Myscript.sh).现在它将输出打印到console.Upto现在它正常工作.
如果我写一个像下面的程序(抛出一些异常)
public class EchoTest {
public static void main (String args[]) {
System.out.println ("Value is "+(2/0));
}
Run Code Online (Sandbox Code Playgroud)
它只是将java异常打印到控制台上.但是我的要求是我希望它在控制台上打印0或1,即当我的java程序失败时我希望得到0(零)并且想要在java程序成功执行时得到1(一).
我需要一个Java程序将任何文件打印到默认/选定的打印机(实际上我使用RICOH通用PostScript打印机将任何文件打印到PostScript文件).此外,我将从打印机读取该流并将其写入PostScript文件.我已经尝试过谷歌下面的示例程序了,但是当我收到PostScript文件时,它是以某种未知的格式.
public class PrintToFileWithJava {
private static boolean jobRunning = true;
public static void main(String[] args) throws Exception {
InputStream is = new BufferedInputStream(new FileInputStream("Authentication in Hive.pdf"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = service.createPrintJob();
printJob.addPrintJobListener(new JobCompleteMonitor());
Doc doc = new SimpleDoc(is, flavor, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
printJob.print(doc, attributes);
while (jobRunning) {
Thread.sleep(1000);
}
System.out.println("Exiting app");
is.close();
}
private static class JobCompleteMonitor extends PrintJobAdapter {
@Override
public void printJobCompleted(PrintJobEvent jobEvent) {
System.out.println("Job completed");
jobRunning …Run Code Online (Sandbox Code Playgroud) 我想根据长度对java中的字符串进行子串(起始字符)。例如,如果 string1 大于 4000 字节,我想将该字符串变成小于或等于 4000 字节的字符串。(需要修剪起始字符而不是最后一个字符)