How to send a text file to a printer?
Hi, I'm in dark on how to send a text file to a printer through Java Stored Procedure.
Here are what I tried so far (OS: win 2000, Oracle: 817 or 9i2):
1, Enable DOS command in Java Stored Proc. and try to send PRINT command there:
public static String Run(String Command){
try{
Runtime.getRuntime().exec(Command);
System.out.println("Command: " + Command);
return("0");
}
catch (Exception e){
System.out.println("Error running command: " + Command +
"\n" + e.getMessage());
return(e.getMessage());
}
}
public static void main(String args[]){
if (args.length == 1)
Run("print /D:\\\\enterprise\\john " + args[0]);
else System.out.println("Usage: java OSCommand filename");
}
PL/SQL wrapper:
CREATE OR REPLACE FUNCTION OSCommand_Run(p1 IN VARCHAR2) RETURN VARCHAR2 AUTHID CURRENT_USER AS LANGUAGE JAVA NAME 'OSCommand.Run(java.lang.String) return java.lang.String';
SQL command:
//print /D:\\machine\printer test.txt
I loaded the Java Stored Procedure into SYSDBS account, it failed silently, even though piece of code works fine in external JVM.
2, Use filePrinter:
public static String print(String printString) {
try{
String printerUNC = "\\\\machine\\printer";
FileWriter fw = new FileWriter(printerUNC);
PrintWriter pw = new PrintWriter(fw);
pw.println(printString);
fw.close();
return "OK";
}catch(Exception e){
e.printStackTrace();
return "Exception: " + e.getMessage();
}
}
public static void main(String[] args) {
try{
String printerUNC = "\\\\machine\\printer";
String printString = "Hello World";
FileWriter fw = new FileWriter(printerUNC);
PrintWriter pw = new PrintWriter(fw);
pw.println(printString);
fw.close();
}catch(Exception e){e.printStackTrace();}
}
I loaded it into SYSDBS too, and tried with this:
SQL> select MY_PRINT.PRINT('HELLO from Oracle') from dual;
MY_PRINT.PRINT('HELLOFROMORACLE')
----------------------------------------------------------
Exception: No such file or directory
SQL> show user
USER is "SYS"
It works in external JVM too.
What's wrong with this?
Thanks for any help in advance,
Charles