4

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

我也可以在 Process Explorer 中看到它——“当前目录”是 exe 的位置。
有没有办法改变它?

(如果我不够清楚 - 我想将我的应用程序放在中央网络位置而不是每个用户文件夹中 - 但我希望它运行 - 通过在每个用户文件夹中放置一个快捷方式在每个用户文件夹上方。)

顺便说一句:我为什么不通过代码编写来解决它?由于我的 exe 中有第三方 jars(我正在使用 exe4j 制作一个 exe)

4

4 回答 4

5

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.
于 2011-12-07T07:37:03.163 回答
1

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

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

并在您的程序中获取此属性:

System.getProperty("myproperty");

您还可以设置多个系统属性。

于 2011-12-11T02:41:30.200 回答
0

您可以以编程方式破解类路径,这将允许您指定特定文件夹或一系列文件夹来访问数据。

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:\ 添加到类路径中。

于 2011-12-11T11:06:50.597 回答
0

我会通过 cmd 或 bat 文件启动 java 应用程序,然后在调用 javaw 之前切换到工作目录。如果您在 java 应用程序代码中没有做任何特别的事情,那么其中的所有路径都将与您启动 java 的位置相关。

杰斯

于 2011-12-09T08:44:59.313 回答