微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更改程序的运行目录 – 通过在exe快捷方式中设置它?

我有一个快捷方式运行的exe进程。

在快捷方式的“开始”属性中,我将其设置为所有应用程序资源所在的文件夹。 该进程仍然在exe的位置查找文件,而不是快捷方式中写入的位置。

我也可以在Process Explorer中看到它 – “当前目录”是exe文件的位置。

有没有办法改变它?

(如果我不够清楚 – 我想把我的应用程序放在一个中央networking位置,而不是在每个用户文件夹中 – 但是我希望它运行 – 在每个用户文件夹中放一个快捷方式,在每个用户文件夹上方。

顺便说一句:为什么我不能用代码写入来解决它? 因为我的exe文件中有第三方的jar文件(我正在使用exe4j来制作exe文件

了解Linux目录权限推理

好的C ++目录和文件库?

如何通过batch file列出所有具有大小的文件

如何检查目录是否包含Powershell中的另一个目录?

有没有办法用Javalocking一个目录?

什么是__dirstream,我们在哪里可以find定义

帮助inotify C脚本

在virtalenv中运行一个现有的python web应用程序

我如何获得目录中的所有文件名?

如何使用C ++来复制目录

从exe4-j文档看来,这似乎可以在exe4j项目中配置。

Working directory For some applications (especially GUI applications) you might want to change the working directory to a specific directory relative to the executable,for example to read config files that are in a fixed location. To do so,please select the Change working directory to: checkBox and enter a directory relative to the executable in the adjacent text field. To change the current directory to the same directory where the executable is located,please enter a single dot.

一种替代方法是使用系统属性。 只需创建一个这样的捷径:

java -Dmyproperty="\myservermyfolder" -jar yourjar.jar

并获得您的程序的这个属性

System.getProperty("myproperty");

您也可以设置多个系统属性

我将通过cmd或bat文件启动java应用程序,然后在调用javaw之前更改为工作目录。 如果你没有在你的java应用程序代码中做任何特殊的事情,那么它的所有路径将与你启动java的地方有关。

枷锁

你可以通过编程来破解类路径,这将允许你指定一个特定的文件夹或一系列文件夹来访问数据。

import java.io.IOException; import java.io.File; import java.net.urlclassloader; import java.net.URL; import java.lang.reflect.Method; public class ClasspathHacker { private static final Class[] parameters = new Class[]{URL.class}; public static void addFile(String s) throws IOException { File f = new File(s); addFile(f); }//end method public static void addFile(File f) throws IOException { addURL(f.toURI().toURL()); }//end method public static void addURL(URL u) throws IOException { urlclassloader sysloader = (urlclassloader) ClassLoader.getSystemClassLoader(); Class sysclass = urlclassloader.class; try { Method method = sysclass.getDeclaredMethod("addURL",parameters); method.setAccessible(true); method.invoke(sysloader,new Object[]{u}); } catch (Throwable t) { t.printstacktrace(); throw new IOException("Error,Could not add URL to system classloader"); }//end try catch }//end method }//end class

用的属性加载器文件

import java.io.InputStream; import java.util.Enumeration; import java.util.Locale; import java.util.Properties; import java.util.ResourceBundle; public abstract class PropertyLoader { /** * Looks up a resource named 'name' in the classpath. The resource must map * to a file with .properties extention. The name is assumed to be absolute * and can use either "/" or "." for package segment separation with an * optional leading "/" and optional ".properties" suffix. Thus,the * following names refer to the same resource: * <pre> * some.pkg.Resource * some.pkg.Resource.properties * some/pkg/Resource * some/pkg/Resource.properties * /some/pkg/Resource * /some/pkg/Resource.properties * </pre> * * @param name classpath resource name [may not be null] * @param loader classloader through which to load the resource [null * is equivalent to the application loader] * * @return resource converted to java.util.Properties [may be null if the * resource was not found and THROW_ON_LOAD_FAILURE is false] * @throws IllegalArgumentException if the resource was not found and * THROW_ON_LOAD_FAILURE is true */ public static Properties loadProperties (String name,ClassLoader loader) { if (name == null) throw new IllegalArgumentException ("null input: name"); if (name.startsWith ("/")) name = name.substring (1); if (name.endsWith (SUFFIX)) name = name.substring (0,name.length () - SUFFIX.length ()); Properties result = null; InputStream in = null; try { if (loader == null) loader = ClassLoader.getSystemClassLoader (); if (LOAD_AS_RESOURCE_BUNDLE) { name = name.replace ('/','.'); // Throws MissingResourceException on lookup failures: final ResourceBundle rb = ResourceBundle.getBundle (name,Locale.getDefault (),loader); result = new Properties (); for (Enumeration keys = rb.getKeys (); keys.hasMoreElements ();) { final String key = (String) keys.nextElement (); final String value = rb.getString (key); result.put (key,value); } } else { name = name.replace ('.','/'); if (! name.endsWith (SUFFIX)) name = name.concat (SUFFIX); // Returns null on lookup failures: in = loader.getResourceAsstream(name); if (in != null) { result = new Properties (); result.load (in); // Can throw IOException } } } catch (Exception e) { result = null; } finally { if (in != null) try { in.close (); } catch (Throwable ignore) {} } if (THROW_ON_LOAD_FAILURE && (result == null)) { throw new IllegalArgumentException ("Could not load [" + name + "]"+ " as " + (LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle" : "a classloader resource")); } return result; } /** * A convenience overload of {@link #loadProperties(String,ClassLoader)} * that uses the current thread's context classloader. */ public static Properties loadProperties (final String name) { return loadProperties (name,Thread.currentThread ().getContextClassLoader ()); } private static final boolean THROW_ON_LOAD_FAILURE = true; private static final boolean LOAD_AS_RESOURCE_BUNDLE = false; private static final String SUFFIX = ".properties"; } // End of class

那么你可以添加一个路径如下

try { //First Load up the properties and populate the config ClasspathHacker.addFile("/pathtomyapp"); } catch (IOException ex) { ex.printstacktrace(); } properties = PropertyLoader.loadProperties("myapp");

或者你也可以使用getResourceBundle来获取你的资源,这只是黑客入侵类路径的一个例子,允许文件可用,你总是可以通过编程方式添加类路径,让你需要的jar文件可以驻留在那里,所以如果您始终确保应用程序网络路径是Q:您可以将Q:添加到类路径中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐