Previous pdf in response - java

I have servlet, which create html-file and then convert it to pdf-file:
private void ConvertHTMLtoPDF(String sConvertationProgramm, String sHTML, String sPDF)
{
try {
ProcessBuilder pb = new ProcessBuilder(sConvertationProgramm, sHTML, sPDF);
Process process = pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Everything work perfect, but then I open this new (as I think) generated pdf-file from this code:
ConvertHTMLtoPDF("C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe",
"PDFtemplate/requiredPDF.html",
"PDFtemplate/Report.pdf");
response.sendRedirect("PDFtemplate/Report.pdf");
- that gave me previous pdf-file, which was created before.
Furthermore I've tried to open it from Windows explorer, and it's also showed me previous file every first opens.
Does anybody have any suggestion what's happen and how to solve this problem?
Any input would be greatly appreciated,
Thanks

You are doing response.sendRedirect("PDFtemplate/Report.pdf") after starting some process. You should wait finish of that process and only after finishing make other actions.
Use process.waitFor();

Are you sure you are replacing your current file with the new one, I doubt you are writing your new file into this location.

Related

java BufferdImage dosen´t load

So right now I´m trying to load an Image on my Harddrive in a BufferdImage in my Code. Yet I think I did everything right but my trycatch only leaves the catch.
Code for better understanding :
private static BufferedImage image;
public void initPictures() {
try {
image = ImageIO.read(new File("Pictures/blue.png"));
} catch (IOException ex) {
System.out.println("Will not load");
ex.printStackTrace();
}
}
The initPictures() is called in my Constructor of the Class. And you can see here that my picture that I try to load is in the E:\Dropbox\Dropbox\Java Projekte\FallingBlocks1\build\classes\Pictures folder on my OS. So the "Pictures/blue.png" should be good.
Windows Folder Picture:
So my Qustion here is : What am I missing?
EDIT : added ex.printStackTrace(); nothing changed thought.
EDIT : also tried to load other pictures int the "image", with no other results
There are two possibilities:
1. Your program may not be running in the folder that contains the "Pictures" folder.
To find out where your program is running, do a System.out.println(System.getProperty("user.dir")); to see what folder you're running in. If it prints something other than E:\Dropbox\Dropbox\Java Projekte\FallingBlocks1\build\classes, you know it's an issue with the path you're providing to ImageIO.read(file). For kicks, you may also want to see what new File("Pictures/blue.png").isFile() is returning.
2. Your image might not readable.
If you try the first test out and you know for certain that the path you are providing is correct, then it's likely that your image file is either corrupt or cannot be read (could be caused by access restrictions, connectivity problems, hardware issues, etc.). Try pointing to a different image and see what happens.

Java application sometimes hangs at start

It's a really weird issue:
Sometimes when I try to start my app, the process creates himself but doesn't do anything.
And sometimes it starts.
I tried to look on the web but I didn't find anything helpful for now. So I was wondering if any of you had the same problem and how did you solved it.
PS:I tried to see the console and show printStackTrace but, there wasn't anything. Just the regulars logs until it hangs (before showing any GUI).
Edit:
Here is the cmd with java -jar when it happens
And Task manager stuck at 30 000k memory
My program is just calling : read("MaxAttempts")
public String read(String NomFonction) {
String ConfigFile = cfgfile.getPath();
try{
InputStream flux=new FileInputStream(ConfigFile);
InputStreamReader lecture=new InputStreamReader(flux);
BufferedReader buff=new BufferedReader(lecture);
String ligne;
String Fonction = null;
while ((ligne=buff.readLine())!=null){
if(ligne.contains(NomFonction + "=")) {
Fonction = ligne.split("=")[1];
}
}
buff.close();
if(!Fonction.equals(null)) {
return Fonction;
}
} catch(Exception e1) {
e1.printStackTrace();
}
return "Error";
}
Try to reproduce it in the debug mode.
Keep starting the process in the debug mode until it is reproduced. When the program hangs, see the list of your threads. If the configuration reading happens in the main thread (and it looks so. If not, pick up the required thread), depending on your IDE, right click on the tread and select "suspend" (then again, depending on your IDE).
It will show where your thread hung.

file.delete() not working properly

I'm having some problems with file deletion in Java. I'm doing a simple CRUD application for my OOP class and we need to use files as database. I tried to create a function to erase the database (Delete all database files and save new empty ones), but, so far, I always receive the success message, but the files still there, untouched.
I have the file path set this way in my config.java file:
private String fileBooks = "books.dat";
private String fileUsers = "users.dat";
private String fileOperations = "operations.dat";
I have this function, responsible for file erase:
public void deleteFiles() {
try {
File fbooks = new File(config.getFileBooks());
File fusers = new File(config.getFileUsers());
File fop = new File(config.getFileOperations());
if(fbooks.delete() && fusers.delete() && fop.delete()){
JOptionPane.showMessageDialog(null, "Success!",
"Database cleaning", JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "Error!",
"Database cleaning", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I checked the inheritance, the controller, everything looks fine. What I'm doing wrong? Is it a problem with the path?
If is useful information I'm running a Ubuntu 14.04 LTS, using NetBeans to code.
Any help and code advice will be welcome.
EDIT:
I added the System.out.println(fbooks.getAbsoluteFile()) this was my output:
/home/dotk/Dropbox/College/OOP/TP02/hw02/books.dat
/home/dotk/Dropbox/College/OOP/TP02/hw02/users.dat
/home/dotk/Dropbox/College/OOP/TP02/hw02/operations.dat
Do I need to change the path? I want the program to be OS independent, and it will be probably tested in a windows machine.
EDIT 2
I changed the file names, now they don't have the "./" but still not working. I verified that isn't any other open streams to the files, the only ones that run are in the start of the program to load the informations in ArrayLists, but they are closed after.
I found the error, the problem was in the order of the calls. I feel a little bit stupid, but now is fixed.

Way to create an exception when program forced to exit?

I'm trying to make a simple Java wordcounting program.
When it reads Microsoft Office files, it first reads the text of the XML files (Microsoft Office files are actually bundles of zipped xml files!) and then reads them to a folder called "converted".
I want "converted" to be deleted right after the program ends, so I added a
new File("converted").deleteOnExit();
which does that well.
However, if the user of the program presses Ctrl+C in the command prompt, then the program will exit early, and the folder will not be deleted.
I would like to know if there's a way to throw an exception if a program is exited. It seems unlikely, because a forced exit of a program will probably stop any code, but I was wondering if this is possible. Then, I'll be able to handle the exception and exit the program correctly, so that the directory will be deleted. I mean, I can add this:
catch(ExitException e) { // if the exception is called "ExitException"
System.err.format("Program ended unexpectedly.%n");
System.exit(-1); // this line so that the folder can delete
}
(The way I understand it, the folder is only deleted if a System.exit() is called. Correct me if I'm wrong.)
Thanks.
There isn't really a way to have it throw an exception on ^C (Control C), but you can have code run when the program is exited in any way as seen below.
Try using shutdown hooks: Runtime.getRuntime().addShutdownHook() should run even on ^C.
Note that this won't run under very specific cases as defined there (SIGKILL), but it will handle most lower things.
Try this instead (run it once at some point in your program):
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
new File("converted").delete();
} catch (Exception e) {
e.printStackTrace();
}
}
});
And get rid of your new File("converted").deleteOnExit();.

Java App to run a Kettle Job and receive the result

It is possible to run a Kettle Job/Transformation from a Java application, and then get the result (a variable for example), in the same Java App?
Although command line execution from Java likely isn't ideal, below would work. Just replace the cmd line with the appropriate paths and read in the output file of the job.
public static void main(String[] args) {
try {
String cmd = "\"c:\\Program Files\\Pentaho\\pdi-ce-5.0.1.A-stable\\data-integration\\kitchen.bat\" -file=\"c:\\users\\exampleuser\\desktop\\examplejob.kjb\"";
System.out.println(cmd);
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (Exception e) {
e.printStackTrace(System.err);
}
/*READ OUTPUT FILE OF KJB IN TO OBTAIN VALUES*/
}
*http://forums.pentaho.com/showthread.php?81151-Tutorial-Using-command-line-arguments-with-Kettle-and-scheduling
*http://docs.oracle.com/javase/tutorial/java/data/strings.html
*http://wiki.pentaho.com/display/EAI/.01+Introduction+to+Spoon
*http://javarevisited.blogspot.com/2011/02/how-to-execute-native-shell-commands.html
*http://www.mkyong.com/java/how-to-execute-shell-command-from-java/
I'd try this to execute the transformation this way. Kettle's Java API is quite readable.
The way described above does not cover retrieving the results. You'd need to add some custom logic to read the output data of the job. I'm not sure if there is a generic way to do that with ETL.
You can try grabbing the output into a HSQLDB or another in-memory DB, as described here, and extract the output manually.

Categories