Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to send a text file to a printer?

98161Nov 18 2002
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 18 2002
Added on Nov 18 2002
2 comments
728 views