How can I change the current program directory - by setting it in the exe shortcut?

I have an exe process that works with a shortcut.
In the โ€œStart atโ€ property of the shortcut, I install it in the folder where all the application resources are located. This process is still looking for files at the exe location, not at the location indicated in the shortcut.

I also see it in Process Explorer - the "current directory" is the location of the exe.
Is there any way to change it?

(If I werenโ€™t clear enough - I want to place my application in a central network location, and not in each user folder, but I want it to run - above each user folder, putting a shortcut in each user folder.)

BTW: Why don't I solve it by writing code? Due to third party banks in my exe (I use exe4j to create exe)

+4
source share
4 answers

From exe4-j documentation .. , it looks like this can be configured in exe4j project.

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. 
+5
source

One option is to use a system property. Just create a shortcut as follows:

 java -Dmyproperty="\\myserver\myfolder" -jar yourjar.jar 

And get this property in your program:

 System.getProperty("myproperty"); 

You can also set several system properties.

+1
source

I ran the Java application through the cmd or bat file and then go to the working directory before you call javaw. If you are not doing anything special in your Java application code, all the paths in it will refer to the place where you started java.

Jess

0
source

You can program a class path that allows you to specify a specific folder or series of folders for accessing data.

 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 

with property loader file

 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 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 

then you can add the path as follows

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

or you can also use getResourceBundle to get your resources, this is just one example of hacking the class path to allow access to files, you can always just add the class path programmatically and let the jar files you need to put there, therefore, if you always guarantee that the network path of the application is Q: you can add Q: \ to the classpath.

0
source

Source: https://habr.com/ru/post/1383637/


All Articles