简体   繁体   中英

How can I get path java source code in runtime?

Is there no way to get path of Java source code in runtime?

Python can get it's path in runtime by using below code

import os
file_path = (os.path.dirname(os.path.realpath(__file__)) )

Many people saids I can get path by using System.getProperty("user.dir") or new File(".").getAbsolutePath() .
But both method returns the path where java source code is executed...

For example, if source code is in /User/Desktop/Project/src/test.java and execute it at /User/Desktop , it returns /User/Desktop instead of /User/Desktop/Project/src/test.java .

Is there no way to get path of source file?
⋇This java file works as servlet to handle AJAX request


Addition - Purpose of finding source code path

I wrote python script and executing it by using Runtime().getRuntime().exec() .
I want to find python script without absolute path (I don't like path hardcoding).
So I'm trying to find java source code path and compute python source code path based on java path.

您不能在 java 中执行此操作,因为(与 Python 不同)java 是编译语言,源代码通常不捆绑在应用程序中。

Maybe we can use the script "echo $PATH", it will print the whole Path that include the "JAVA_HOME" if you are run on Linux. The batch script for Windows is "where java", it will return the java's path. Hopes it can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM