|
Ahoj, ja to resim takto. Je to mnou upravene reseni, ktere
jsem tusim nekde nasel ve forech na Sunu. Neni to prilis elegantni a robustni,
ale funguje. Mozna by to stalo za nejaky feature request.
import java.io.*;
import java.net.URL; import java.net.URLDecoder; /**
* Pomocna trida pro identifikaci adresare ve kterem je program spusten * @author Vity */ final class AppPrefs { private static String appPath = null; private static final String CLASS_EXT =
".class";
private static final String JAR_SEPARATOR = "!/"; private static final String URL_SEPARATOR = "/"; private static final String CLASS_SEPARATOR = "."; private static final String FILE_PREFIX = "file:"; /**
* Konstruktor */ private AppPrefs() { } /**
* Vraci absolutni cestu k aktualnimu adresari, kde je program spusten * @return absolutni cesta */ public static String getAppPath() { if (appPath != null) return appPath; final int end; String urlStr; String clsName = AppPrefs.class.getName(); final int clsNameLen = clsName.length() + CLASS_EXT.length(); int pos = clsName.lastIndexOf(CLASS_SEPARATOR); if (pos > -1) { clsName = clsName.substring(pos + 1); } final URL url = "" + CLASS_EXT); if (url != null) { urlStr = url.toString(); pos = urlStr.indexOf(JAR_SEPARATOR); if (pos > -1) { urlStr = urlStr.substring(0, pos); end = urlStr.lastIndexOf(URL_SEPARATOR) + 1; } else { end = urlStr.length() - clsNameLen; } pos = urlStr.lastIndexOf(FILE_PREFIX); if (pos > -1) { pos += FILE_PREFIX.length() + (isWindows() ? 1 : 0); } else { pos = 0; } urlStr = urlStr.substring(pos, end); String decoded = ""; try
{
decoded = URLDecoder.decode(urlStr, "ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return appPath = decoded; } return ""; }
/**
* Identifikuje, zda je program spusten na windows * @return true pokud je program spusten na windows, jinak false */ private static boolean isWindows() { final String osName = System.getProperty("os.name"); return (osName == null || osName.startsWith("Windows")); } } From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Lukas Adamek | Fayn Telecommunications s.r.o. Sent: Sunday, October 29, 2006 1:45 PM To: [email protected] Subject: Problem s cestou k souboru, ktery je mimo jar Ahoj mam nasledujici
problem: Mam aplikaci, ktera vypisuje sve
logy do externiho souboru. Chci aby tento logovaci soubor byl
ulozen ve stejnem adresari, jako je ulozeny jar cele
aplikace. A tady nastava problem.
Pokud se totiz snazim odkazovat mimo
jar, haze aplikace chybu. Pokud nejsou .class soubory v jaru
zabaleny, vse funguje. Jak toto
resite? Vypada to nejak
takto: --------------------------------- package
jedenbalik; public class MojeTrida
{
public MojeTrida
() throws URISyntaxException, IOException {
URI i = new URI(
getClass().getResource(“../”).toString() + “soubor.log” ); // Zde to vyhazuje
chybu
File file = new File(uri);
FileInputStream fis;
try {
fis = new FileInputStream(file);
// dalsi kod
} catch (FileNotFoundException ex) {
// odchyceni vyjimek
}
} |
- Problem s cestou k souboru, ... Lukas Adamek | Fayn Telecommunications s.r.o.
- RE: Problem s cestou k ... Vity
- Re: Problem s cestou k ... Pavel Stastny
- RE: Problem s cestou k ... Podlesak Kamil
