log4jを使用しているJavaアプリケーションを作成しています。構成log4jファイルの絶対パスと、生成されたログファイル(このログファイルが生成される場所)の絶対パスを指定しました。実行時にJavaWebアプリケーションの絶対パスを取得するには、次の方法を使用します。
String prefix = getServletContext().getRealPath("/");
しかし、通常のJavaアプリケーションのコンテキストでは、何を使用できますか?
試す;
String path = new File(".").getCanonicalPath();
あなたが何を求めているのかは明確ではありません。getServletContext().getRealPath()
答えがない場合、「使用しているWebアプリケーションに関して」が何を意味するのかわかりませんが、次のようになります。
System.getProperty("user.dir")
System.getProperty("user.home")
this.getClass().getProtectionDomain().getCodeSource().getLocation()
ます。そして、使用するのはthis.getClass().getProtectionDomain().getCodeSource().getLocation()
どうですか?
aのアプリケーションパスとJAR
内部から実行されるアプリケーションIDE
が異なるため、正しい現在のディレクトリを一貫して返すために次のコードを記述しました。
import java.io.File;
import java.net.URISyntaxException;
public class ProgramDirectoryUtilities
{
private static String getJarName()
{
return new File(ProgramDirectoryUtilities.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath())
.getName();
}
private static boolean runningFromJAR()
{
String jarName = getJarName();
return jarName.contains(".jar");
}
public static String getProgramDirectory()
{
if (runningFromJAR())
{
return getCurrentJARDirectory();
} else
{
return getCurrentProjectDirectory();
}
}
private static String getCurrentProjectDirectory()
{
return new File("").getAbsolutePath();
}
private static String getCurrentJARDirectory()
{
try
{
return new File(ProgramDirectoryUtilities.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();
} catch (URISyntaxException exception)
{
exception.printStackTrace();
}
return null;
}
}
単に電話するだけgetProgramDirectory()
で、どちらの方法でもうまくいくはずです。
Webアプリケーションについて話している場合はgetRealPath
、オブジェクトからを使用する必要がありServletContext
ます。
例:
public class MyServlet extends Servlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String webAppPath = getServletContext().getRealPath("/");
}
}
お役に立てれば。
このメソッドを使用して、jarまたはexeへの完全なパスを取得します。
File pto = new File(YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
pto.getAbsolutePath());
new File(".").getAbsolutePath()
アプリのどこよりも、user.homeのサブディレクトリにファイルを保存することをお勧めします。常駐する可能性があります。
Sunは、アプレットとアプリを確保するためにかなりの努力を払いました。Java Web Startを使用して起動すると、アプリを判別できません。実際のパス。この変更により、多くのアプリが破損しました。変更が他のアプリに拡張されても驚かないでしょう。
/*****************************************************************************
* return application path
* @return
*****************************************************************************/
public static String getApplcatonPath(){
CodeSource codeSource = MainApp.class.getProtectionDomain().getCodeSource();
File rootPath = null;
try {
rootPath = new File(codeSource.getLocation().toURI().getPath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootPath.getParentFile().getPath();
}//end of getApplcatonPath()
Spring(サーブレット)などのJava Webアプリケーションの実際のパスを取得する場合は、HttpServletRequestに付属するサーブレットコンテキストオブジェクトから取得できます。
@GetMapping("/")
public String index(ModelMap m, HttpServletRequest request) {
String realPath = request.getServletContext().getRealPath("/");
System.out.println(realPath);
return "index";
}
私は誰もがこれに関する重要な問題を見逃していると思います。
String prefix = getServletContext().getRealPath("/");
サーブレットインスタンスは、任意の数のノードの1つに存在する可能性があり、技術的にはファイルシステムをまったく必要としません。
たとえば、Java EEコンテナでは、アプリケーションをデータベースまたはディレクトリサーバーからロードできます。アプリケーションのさまざまな部分をさまざまなノードで実行することもできます。アプリケーションの外部へのアクセスは、アプリケーションサーバーによって提供されます。
従来のlog4jとの互換性を維持する必要がある場合は、java.util.loggingまたはApacheCommonsLoggingを使用してください。ログファイルの保存先をアプリケーションサーバーに通知します。
この回答を使用したい場合: https ://stackoverflow.com/a/4033033/10560907
次のようなインポートステートメントを追加する必要があります。
import java.io.File;
非常に最初のJavaソースコード。
クラスパスのルートにファイル「cost.ini」があります。私のJARファイルの名前は「cost.jar」です。
次のコード:
try {
//JDK11: replace "UTF-8" with UTF_8 and remove try-catch
String rootPath = decode(getSystemResource("cost.ini").getPath()
.replaceAll("(cost\\.jar!/)?cost\\.ini$|^(file\\:)?/", ""), "UTF-8");
showMessageDialog(null, rootPath, "rootpath", WARNING_MESSAGE);
} catch(UnsupportedEncodingException e) {}
から返されるパス.getPath()
の形式は次のとおりです。
file:/C:/folder1/folder2/cost.jar!/cost.ini
/C:/folder1/folder2/cost.ini
アプリケーションがJAR形式で提供されている場合、を使用するたびFile
に例外が発生します。
表現
new File(".").getAbsolutePath();
JVMの実行に関連付けられている現在の作業ディレクトリを取得します。ただし、JVMは、
System.getProperty(propertyName);
インターフェース。これらのプロパティのリストは、ここにあります。
これらにより、プラットフォームに依存しない方法で、現在のユーザーディレクトリや一時ディレクトリなどを参照できます。