如何通过编程方式读取eclipse.ini文件中的属性?
从Eclipse产品配置文件(eclipse.ini/launcher.ini)读取属性的可行方案
已尝试方法的局限性
Platform.getApplicationArgs()仅能获取应用启动时传入的程序参数,不会包含ini文件中配置的JVM参数、平台启动参数Platform.getCommandLineArgs()仅能获取Eclipse启动器传递给应用层的参数,无法完整覆盖ini中所有配置项,尤其是以-D开头的系统属性、堆内存配置这类JVM级别的参数
可行解决方案
场景1:读取ini中通过-D定义的自定义系统属性
ini文件中所有以-D开头的配置会被自动加载为JVM系统属性,直接调用System.getProperty("你的属性key")即可获取对应值。
示例:如果launcher.ini中配置了-Dcom.yourcompany.product.env=prod,调用System.getProperty("com.yourcompany.product.env")就能直接拿到prod值。
场景2:读取ini中非-D的启动参数(如-Xmx、-startup等)
可以先定位到配置文件路径,再逐行读取解析,示例代码如下:
import org.eclipse.core.runtime.Platform; import org.eclipse.osgi.service.datalocation.Location; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class LauncherIniReader { public List<String> readAllLauncherConfig() throws IOException { List<String> validConfigLines = new ArrayList<>(); // 获取当前产品安装根目录位置 Location installLocation = Platform.getInstallLocation(); String installRootPath = installLocation.getURL().getPath(); // 拼接launcher.ini完整路径,若用eclipse.ini可替换对应文件名 String iniFilePath = installRootPath + "launcher.ini"; // MacOS系统需要调整路径,取消注释下方逻辑即可 // if (Platform.getOS().equals(Platform.OS_MACOSX)) { // iniFilePath = installRootPath + "Contents/Eclipse/launcher.ini"; // } // 逐行读取过滤无效行 try (BufferedReader br = new BufferedReader(new FileReader(iniFilePath))) { String line; while ((line = br.readLine()) != null) { String trimLine = line.trim(); // 过滤注释行和空行 if (!trimLine.startsWith("#") && !trimLine.isEmpty()) { validConfigLines.add(trimLine); } } } return validConfigLines; } }
获取到所有有效配置行后,按前缀匹配即可提取你需要的任意参数。
内容的提问来源于stack exchange,提问作者Elena Larina
相关产品推荐
相关产品推荐

