localisation fichier selon méthode lancement .jar
Bonjour, je travaille sur un programme qui génère régulièrement un fichier XML après avoir lu une base de données.
La problématique qui me concerne est la localisation de ce fichier.
Quand je travaille sous Eclipse: il prend le dossier courant. :ccool:
Quand je travaille sur le .jar généré en lançant de cette façon
Code:
1 2
| pushd "%~dp0%"
java -jar prodchecker.jar |
, il prend le dossier courant :ccool:
Mais si je double clique sur le .jar, alors il ne prend pas le dossier courant et je ne trouve pas le fichier.
Voici comment je procède :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
Class class1;
URL url = null;
URL relativeURL = null;
URL relativeURLTemp = null;
File outputFile = null;
File outputTempFile = null;
try {
class1 = Class.forName("core.Main");
url = class1.getResource("");
relativeURL = new URL(url, "../output.xml");
relativeURLTemp = new URL(url, "../outputTemp.xml");
outputFile = new File(relativeURLTemp.getPath());
outputTempFile = new File(relativeURL.getPath());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch(MalformedURLException mue){
} |
puis
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc_);
StreamResult result = new StreamResult(outputTempFile);
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} |
puis (facultatif ici)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
try{
InputStream in = new FileInputStream(outputTempFile);
//For Overwrite the file.
OutputStream out = new FileOutputStream(outputFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
System.out.println("Done");
} |
Comment faire?
Merci!