0

I m searching why my show *.showDialog for a "save to" doesn't work.

It works correctly when i m launching it with my IDE Intellij-idea.

ShowDialog works !

But when i want to start my app with the .jar file, the showDialog doesn't work.

ShowDialog error

It would be run when i click on "Valider".

This is my code:

JFileChooser chooser = new JFileChooser();

chooser.setCurrentDirectory(new File("."+File.separator));

int reponse = chooser.showDialog(chooser, "Enregistrer sous");

if(reponse == JFileChooser.APPROVE_OPTION)  {
    String fichier = chooser.getSelectedFile().toString();

    document.save(fichier+".pdf");
}

Edit:

This is the new code:

JFileChooser chooser = new JFileChooser();

chooser.setCurrentDirectory(new File(System.getProperty("user.home")));

int reponse = chooser.showDialog(chooser, "Enregistrer sous");

if(reponse == JFileChooser.APPROVE_OPTION)  {
    String fichier = chooser.getSelectedFile().toString();
    document.save(fichier+".pdf");
}

Edit 14/03:

I have found my error:

"javax.imageio.IIOException: Can't read input file!"

I'm trying to resolve this one, it probably because the file path is uncorrectly defined.

This is my code to draw my image:

PDImageXObject pdImage = PDImageXObject.createFromFile("myImage.png", document);
contentStream.drawImage(pdImage, 480, 720);

Thanks

A.Rouze
  • 51
  • 1
  • 10
  • 1
    What means "doesn't work"? Any exception? – Jens Mar 13 '17 at 08:33
  • It could be a permissions issue, in that case some exceptions should be thrown. Try wrapping it up in a try catch statement: – zakaria amine Mar 13 '17 at 08:36
  • 2
    `new File("."+File.separator)` this will likely point to different places between running in the IDE and a built Jar. I prefer to start most dialogs off in `user.home`. – Andrew Thompson Mar 13 '17 at 08:43
  • It's on a try and catch statement. But there is no exceptions issues... I am trying to launch it by my terminal. It doesn't work: "Permission denied". I change the "chmod", but now it doesn't find the main class! – A.Rouze Mar 13 '17 at 09:13
  • 1
    Tip: Add @Jens (or whoever, the `@` is important) to *notify* the person of a new comment. What happened when you tried setting the directory to `System.getProperty("user.home")`? It's one of the few places that ***all*** apps. should have access to. – Andrew Thompson Mar 13 '17 at 10:55
  • @AndrewThompson ( ;) thanks) It works correctly only when i run with my IDE. If i launch my app with the .Jar File after a build artefact, it should be work isn't it ? – A.Rouze Mar 13 '17 at 11:10
  • *"It works correctly only when.."* [edit] to show the new code. – Andrew Thompson Mar 13 '17 at 12:52
  • @AndrewThompson It's update :) – A.Rouze Mar 13 '17 at 14:32
  • So when you run the Jar from the command line, what is the (copy/pasted) output on clicking the 'show dialog' button? – Andrew Thompson Mar 13 '17 at 15:23
  • int reponse = chooser.showDialog(chooser, "Enregistrer sous"); is based on chooser.showDialog(Component parent, approveButtonText) . Probably it gets confused, when parent is itself. Give it a try with int reponse = chooser.showDialog(null, "Enregistrer sous"); – Fredy Fischer Mar 13 '17 at 15:53
  • 1) *"Probably it gets confused.."* You seem to be getting confused. Don't forget to notify the person of a new comment! 2) Don't put information relevant to the question in code comments where it is easily overlooked. Instead [edit] the question then notify the relevant people in a comment after that is done. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 13 '17 at 22:53
  • @FredyFischer Your solution doesn't work... – A.Rouze Mar 14 '17 at 08:04
  • Thanks @AndrewThompson ! I have succeeded to run my app in the terminal. Now i have seen my error. I'm trying to resolve this one. "javax.imageio.IIOException: Can't read input file!" I think it just a problem with the file path. – A.Rouze Mar 14 '17 at 08:31
  • `javax.imageio.IIOException` Interesting.. Are you using JAI at any point? – Andrew Thompson Mar 14 '17 at 09:01
  • @AndrewThompson I'm using this only to draw one image (.png). See edit for my code. – A.Rouze Mar 14 '17 at 09:36
  • *"javax.imageio.IIOException: Can't **read** input file!"* If that read *"Can't **write** output file"* then this *"I'm using this only to draw one image"*" might be relevant. So far it seems you are very confused, and making random guesses. Try to understand both what the error messages are telling you, and why I (correctly) guess what I do. So.. I guessed that JAI was involved from the fact that the IDE version could ***read*** the image file, but the built Jar apparently cannot. That implies that JAI is on the run-time class path in the IDE, but not when it is run as a Jar. – Andrew Thompson Mar 14 '17 at 10:10
  • JAI gives an app. the ability to read a wider range of image types, than are available in the standard JVM. While `png` file type is technically supported, not all PNG files are the same. They can have different encodings internally. Apparently JAI can read that type of PNG, but the standard JVM cannot. – Andrew Thompson Mar 14 '17 at 10:13
  • @AndrewThompson Ok, so now if i understand, i need to search how run my app with JAI. Or use another technique to print my image in a pdf doc. That's it ? – A.Rouze Mar 14 '17 at 10:42
  • *"Or use another technique to print my image in a pdf doc."* The code is failing before that! See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) *"i need to search how run my app with JAI"* Yes. Good search terms would be 'runtime classpath'. You need to make sure JAI is on that runtime classpath, when run as a Jar outside the IDE. – Andrew Thompson Mar 14 '17 at 11:04
  • @AndrewThompson Hello ! Ok it works ! I must put the absolute path. Thank you ;) – A.Rouze Mar 15 '17 at 07:16
  • *"I must put the absolute path. "* ..that's not the correct way to solve the problem. But if you're happy with it .. (shrugs) – Andrew Thompson Mar 15 '17 at 08:25

0 Answers0