Printing using textPrinter in Ubuntu - java

I using this library for printing, and I am using Ubuntu.
My code is the following:
FilePort port=new FilePort("\\\\printerIp:631\\printers\\Hewlett-Packard-HP-LaserJet-Professional-P1606dn");
TextPrinter printer=PrinterFactory.getPrinter("HP-PCL5");
However I am not able to print. When I use
FilePort port=new FilePort("\home\hi.txt")
the text is getting printed in hi.txt file.
I tried \dev\lpr but it is giving me error
com.java4less.textprinter.exceptions.CouldNotOpenPrinterException: /dev/lpr (Permission denied)
FilePort port=new FilePort("\dev\lpr");
Here is my full source code
try {
FilePort port = new FilePort("\\\\10.9.42.136:631\\printers\\Hewlett-Packard-HP-LaserJet-Professional-P1606dn");
// get printer supporting HP-PCL command set
TextPrinter printer = PrinterFactory.getPrinter("HP-PCL5");
// create printing job
JobProperties job = printer.getDefaultJobProperties();
job.landscape = true;
printer.startJob(port, job);
// print BOLD text
TextProperties prop = printer.getDefaultTextProperties();
prop.bold = true;
printer.printString("This must be BOLD", prop);
printer.newLine();
printer.newLine();
printer.printString("Hello world..", prop);
// print line
printer.printHorizontalLine(5,0,80); // row 5, column 0 to 80
printer.endJob();
} catch (Exception e) {
e.printStackTrace();
}
Please help me.

you need to edit some permissions first, try this
edit the file in /etc/cups/cupsd.conf
search for lpadmin and change it to lp

Related

Named pipe between Rust and Java Ghidra Process Access Rights

I'm trying to create a named Pipe between a Rust program that executes a Ghidra process and the Java Program that is running as part of the Ghidra process. I've currently trouble to get the correct access rights (permission bits).
java.io.FileNotFoundException: /home/.../ghidra_rust_pipe/pipe/.tmpkOE0zg/pcode.pipe (Permission denied)
My Rust program looks like this (I tried to set the permission bits to have write access for other users.)
...
let tmp_dir = TempDir::new_in("pipe").unwrap();
let fifo_path = tmp_dir.path().join("pcode.pipe");
// create new fifo and set permission bits
match unistd::mkfifo(&fifo_path, stat::Mode::S_IWOTH) {
Ok(_) => println!("created {:?}", fifo_path),
Err(err) => println!("Error creating fifo: {}", err),
}
// Execute Ghidra
let output = Command::new(&headless_path)
.arg(tmp_ghidra_project)
.arg("PcodeExtractor")
.arg("-import")
.arg(file_path)
.arg("-postScript")
.arg("PcodeExtractor.java")
.arg(fifo_path.clone()) // Path to the named pipe
.arg("-scriptPath")
.arg(script_path)
.arg("-deleteProject")
.output()
.unwrap();
...
if let Ok(mut file) = File::open(fifo_path) {
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => println!("{}", contents),
Err(err) => panic!("Failed to write contents {}", err),
}
}
In the Java program I try to access access the pipe like this (the path is the pipe parameter given by the Rust program):
...
Gson gson = new GsonBuilder().setPrettyPrinting().addSerializationExclusionStrategy(strategy).create();
try {
FileOutputStream pcodeStream = new FileOutputStream(path);
String jsonString = gson.toJson(project);
pcodeStream.write(jsonString.getBytes());
pcodeStream.flush();
pcodeStream.close();
} catch (JsonIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
...
I already tried some of those codes: https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html. Does anyone know how to set the rights correctly?
Here's the full trace of the Java Exception:
java.io.FileNotFoundException: /home/.../ghidra_rust_pipe/pipe/.tmpmazu66/pcode.pipe (Permission denied)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
at serializer.Serializer.serializeProject(Serializer.java:66)
at PcodeExtractor.run(PcodeExtractor.java:61)
at ghidra.app.script.GhidraScript.executeNormal(GhidraScript.java:379)
at ghidra.app.script.GhidraScript.doExecute(GhidraScript.java:234)
at ghidra.app.script.GhidraScript.execute(GhidraScript.java:212)
at ghidra.app.util.headless.HeadlessAnalyzer.runScript(HeadlessAnalyzer.java:574)
at ghidra.app.util.headless.HeadlessAnalyzer.runScriptsList(HeadlessAnalyzer.java:891)
at ghidra.app.util.headless.HeadlessAnalyzer.analyzeProgram(HeadlessAnalyzer.java:1039)
at ghidra.app.util.headless.HeadlessAnalyzer.processFileWithImport(HeadlessAnalyzer.java:1532)
at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1670)
at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1735)
at ghidra.app.util.headless.HeadlessAnalyzer.processLocal(HeadlessAnalyzer.java:443)
at ghidra.app.util.headless.AnalyzeHeadless.launch(AnalyzeHeadless.java:121)
at ghidra.GhidraLauncher.main(GhidraLauncher.java:82)
For anyone with the same problem, the comment by HHK under my question contains the answer of creating a new thread for the other process, so that the pipe does not block.
See code below:
...
let ghidra_subprocess =
thread::spawn(move || { . . . }); // Put Ghidra or any command command here
if let Ok(mut file) = File::open(fifo_path) {
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => println!("{}", contents),
Err(err) => panic!("Failed to write contents {}", err),
}
}
ghidra_subprocess.join().expect("Ghidra Subprocess panicked!");
...

Using WEKA in Java

I am loading a model which I have saved from the WEKA Explorer to my Java code as seen below. I am now trying to give it an instance in the form of a .arff file so that I can get a prediction however it is giving an output of NaN 0.0 every time.
The prediction should be in the form of Levels (eg. Level 1).
The Screen Shot attached is the output I get.
I also attached another screenshot of a dummy .arff file I am giving the model.
try {
NaiveBayes nb = new NaiveBayes();
nb = (NaiveBayes) weka.core.SerializationHelper.read("Models/NaiveBayesModel.model");
DataSource source1 = new DataSource(final_filePath);
Instances testDataSet = source1.getDataSet();
testDataSet.setClassIndex(testDataSet.numAttributes() - 1);
double actualValue = testDataSet.instance(0).classValue();
Instance newInst = testDataSet.instance(0);
double NaiveBayes = nb.classifyInstance(newInst);
System.out.println(actualValue + " " + NaiveBayes);
} catch (Exception e) {
e.printStackTrace();
}
Output
Input
How can I fix this, please?
Thanks,
Andre

Execute command from Java code deployed on Apache Tomcat

I was trying to convert a DOCX file to PDF file, found this vb script code which perfectly converts DOCX to PDF file, it uses .bat file for file generation. The code can be executed through java.
I am facing a strange problem, when I execute the code on my local machine, the file is generated, but when I deployed the app on Server , the code executes with no errors but the file is not generated.
Do we need any permission to execute commands through java?
Following is the details:
Server Operating system : Windows Server 2012 R2 Standard
Application server :Apache Tomcat 7.0.75
Code:
1)Java
public static void generatePDF() {
try {
File file = new File("C:\\Docx_To_Pdf_Converter\\errorLog.txt");
PrintStream printStreamToFile = new PrintStream(file);
System.setOut(printStreamToFile);
String docToPdf = "C:\\Docx_To_Pdf_Converter\\doc2pdf.bat";
File docPath = new File("C:\\Docx_To_Pdf_Converter\\Letter1.docx");
File pdfPath = new File("C:\\Docx_To_Pdf_Converter\\LetterPDF.pdf");
String command = String.format("%s %s %s", docToPdf, docPath, pdfPath);
Process process = Runtime.getRuntime().exec(command);
// The next line is optional and will force the current Java
//thread to block until the script has finished its execution.
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
and the .bat file code:
#Echo off
pushd %~dp0
cscript C:\Docx_To_Pdf_Converter\doc2pdf.vbs %1 %2
vbscript code which actually converts the docx to pdf
Const wdFormatPDF = 17 ' PDF format.
Const wdFormatXPS = 18 ' XPS format.
Const WdDoNotSaveChanges = 0
Dim arguments
Set arguments = WScript.Arguments
Function CheckUserArguments()
If arguments.Unnamed.Count < 1 Or arguments.Unnamed.Count > 2 Then
WScript.Echo "Use:"
WScript.Echo "<script> input.doc"
WScript.Echo "<script> input.doc output.pdf"
WScript.Quit 1
End If
End Function
// Transforms a doc to a pdf
Function DocToPdf( docInputFile, pdfOutputFile )
Dim fileSystemObject
Dim wordApplication
Dim wordDocument
Dim wordDocuments
Dim baseFolder
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Set wordApplication = CreateObject("Word.Application")
Set wordDocuments = wordApplication.Documents
docInputFile = fileSystemObject.GetAbsolutePathName(docInputFile)
baseFolder = fileSystemObject.GetParentFolderName(docInputFile)
If Len(pdfOutputFile) = 0 Then
pdfOutputFile = fileSystemObject.GetBaseName(docInputFile) + ".pdf"
End If
If Len(fileSystemObject.GetParentFolderName(pdfOutputFile)) = 0 Then
pdfOutputFile = baseFolder + "\" + pdfOutputFile
End If
//' Disable any potential macros of the word document.
wordApplication.WordBasic.DisableAutoMacros
// 'from below line the code does not executes
Set wordDocument = wordDocuments.Open(docInputFile)
wordDocument.SaveAs pdfOutputFile, wdFormatPDF
wordDocument.Close WdDoNotSaveChanges
wordApplication.Quit WdDoNotSaveChanges
Set wordApplication = Nothing
Set fileSystemObject = Nothing
End Function
// ' Execute script
Call CheckUserArguments()
If arguments.Unnamed.Count = 2 Then
Call DocToPdf( arguments.Unnamed.Item(0), arguments.Unnamed.Item(1) )
Else
Call DocToPdf( arguments.Unnamed.Item(0), "" )
End If
Set arguments = Nothing
It is not possible to give you a 100% guaranteed answer as we don't have access to your deployment server, but here's what I think is happening there.
If the .bat file were missing or not executable for some reason, then you would get an IOException in your Java code. Since you didn't get an exception, clearly the .bat file was found and executed.
However, whatever is within the .bat file is not being executed as you expect. Either cscript.exe is missing, or the .vbs file is missing. The way your code is written, you would not be aware of this. All you would see is a non-zero status return from the waitfor() method, and you don't bother to check that. Therefore you have no knowledge of what actually happened.
At the very minimum you should change the waitfor() method invocation to:
int rc = process.waitFor();
System.out.printf("Process returned %d\n", rc);
This will tell you the return status from the attempt to execute the .bat file. If it's not zero, then you have a problem, and I'm 99.999% sure you will find this to be non-zero. To troubleshoot this you will need to capture the output from the command. The following is a highly simplified (as in no error handling; that's up to you) example:
Process proc = Runtime.getRuntime().exec(command);
BufferedReader procOutput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while((line = procOutput.readLine()) != null)
{
System.out.println(line);
// Or whatever you need to do to in your environment, such
// as log the output or examine it to ensure the script did
// what you want
}
int rc = proc.waitFor();
System.out.printf("Process returned %d\n", rc);
NOTE The above is not production-level code, it is only an example of how to use the API. This works ONLY if the external command requires no redirected input; if it does require input then you need to handle input and output in separate threads to prevent deadlocks.
Also, you should consider using ProcessBuilder as it is much more flexible. For example, it allows you to redirect the process' output to append to a log file, which you should probably implement.

how do i get the data from a database and store it into a text file?

I am new to databases in Java and i am trying to export the data from 1 table and store it in a text file. At the moment the code below writes to the text file however all on one line? can anyone help?
My Code
private static String listHeader() {
String output = "Id Priority From Label Subject\n";
output += "== ======== ==== ===== =======\n";
return output;
}
public static String Export_Message_Emails() {
String output = listHeader();
output +="\n";
try {
ResultSet res = stmt.executeQuery("SELECT * from messages ORDER BY ID ASC");
while (res.next()) { // there is a result
output += formatListEntry(res);
output +="\n";
}
} catch (Exception e) {
System.out.println(e);
return null;
}
return output;
}
public void exportCode(String File1){
try {
if ("Messages".equals(nameOfFile)){
fw = new FileWriter(f);
//what needs to be written here
//fw.write(MessageData.listAll());
fw.write(MessageData.Export_Message_Emails());
fw.close();
}
}
Don't use a hard coded value of "\n". Instead use System.getProperty("line.separator"); or if you are using Java 7 or greater, you can use System.lineSeparator();
Try String.format("%n") instead "\n".
Unless you're trying to practice your Java programming (which is perfectly fine of course!), you can export all the data from one table and store it in a file by using the SYSCS_UTIL.SYSCS_EXPORT_TABLE system procedure: http://db.apache.org/derby/docs/10.11/ref/rrefexportproc.html
I'm gonna assume you are using Windows and that you are opening your file with notepad. If that is correct then it is not really a problem with your output but with the editor you are viewing it with.
Try a nicer editor, ie. Notepad++
Do as the other answers suggest and use System.getProperty("line.separator"); or similar.
Use a Writer implementation such as, PrintWriter.
Personally I prefer "\n" over the system line separator, which on Windows is "\r\n".
EDIT: Added option 3

Cannot read file using Scanner, never gets into loop

I am trying to read a text file in order to copy some parts of it into a new text file.
This is how I create my Scanner item :
// folder
File vMainFolder = new File(System.getProperty("user.home"),"LightDic");
if (!vMainFolder.exists() & !vMainFolder.mkdirs()) {
System.out.println("Missing LightDic folder.");
return;
}
// file
System.out.println("Enter the source file's name : ");
Scanner vSc = new Scanner(System.in);
String vNomSource = vSc.next();
Scanner vSource;
try {
vSource = new Scanner(new File(vMainFolder, vNomSource+".txt"));
} catch (final java.io.FileNotFoundException pExp) {
System.out.println("Dictionnary not found.");
return;
}
And this is how I wrote my while structure :
while (vSource.hasNextLine()) {
System.out.println("test : entering the loop");
String vMot = vSource.nextLine(); /* edit : I added this statement, which I've forgotten in my previous post */
}
When executing the program, it never prints "test : entering the loop".
Of course, this file I am testing is not empty, it is a list of words like so :
a
à
abaissa
abaissable
abaissables
abaissai
I don't understand what I did wrong, I've used this method a few times in the past.
Problem solved.
I don't really know why, but I solved the problem changing the encoding of my FRdic.txt file from ANSI to UTF-8, and then the file was read.
Thanks to everyone who tried to help me.
Is it normal that a text file encoded in ANSI is not read by the JVM ?

Categories