Java: get absolute path of project

后端 未结 2 1840
悲&欢浪女
悲&欢浪女 2021-01-16 03:15

I\'m trying to run a exe file in path outside of the current package. My code.java file that runs it is in

%Workspace_path%\\Project\\src\\main\\java\\com\\u         


        
相关标签:
2条回答
  • 2021-01-16 03:36

    The de facto approach to solving this is to bundle the EXE as a classpath resource. It seems you have arranged for this already.

    When working with classpath resources, a mature program should not assume that the resource is in the filesystem. The resources could be packaged in a JAR file, or even in a WAR file. The only thing you can trust at that point is the standard methods for accessing resources in Java, as hinted below.

    The way to solve your problem, then, is to access the resource contents using the de facto standard of invoking Class.getResourceAsStream (or ClassLoader.getResourceAsStream), save the contents to a temporary file, and execute from that file. This will guarantee your program works correctly regardless of its packaging.

    In other words:

    1. Invoke getClass().getResourceAsStream("/program.exe"). From static methods, you can't call getClass, so use the name of your current class instead, as in MyClass.class.getResourceAsStream. This returns an InputStream.
    2. Create a temporary file, preferably using File.createTempFile. This returns a File object identifying the newly created file.
    3. Open an OutputStream to this temp file.
    4. Use the two streams to copy the data from the resource into the temp file. You can use IOUtils.copy if you're into Apache Commons tools. Don't forget to close the two streams when done with this step.
    5. Execute the program thus stored in the temporary file.
    6. Clean up.

    In other words (code snippet added later):

    private void executeProgramFromClasspath() throws IOException {
        // Open resource stream.
        InputStream input = getClass().getResourceAsStream("/program.exe");
        if (input == null) {
            throw new IllegalStateException("Missing classpath resource.");
        }
    
        // Transfer.
        OutputStream output = null;
        try {
            // Create temporary file. May throw IOException.
            File temporaryFile = File.createTempFile(getClass().getName(), "");
    
            output = new FileOutputStream(temporaryFile);
            output = new BufferedOutputStream(output);
            IOUtils.copy(input, output);
        } finally {
            // Close streams.
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    
        // Execute.
        try {
            String path = temporaryFile.getAbsolutePath();
            ProcessBuilder processBuilder = new ProcessBuilder(path);
            Process process = processBuilder.start();
            process.waitFor();
        } catch (InterruptedException e) {
            // Optional catch. Keeps the method signature uncluttered.
            throw new IOException(e);
        } finally {
            // Clean up
            if (!temporaryFile.delete()) {
                // Log this issue, or throw an error.
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-16 03:56

    Well,in your context,the project root is happen to be the current path

    .

    ,that is where the java.exe start to execute,so a easy way is:

    String exePath="src\\main\\resources\\program.exe";
    File exeFile=new File(".",exePath);
    System.out.println(exeFile.getAbusolutePath());
    ...
    

    I tested this code on Eclipse,It's ok. I think is should work on different ide. Good Luck!

    0 讨论(0)
提交回复
热议问题