hello!.
i need help with import in java.
i want for example when i click a button in java to open a exe file,so i imported the exe into java,and i make the program everything is okay.
so i export my classes into a jar file,so when i click the button nothing will happened because "the path" of the exe file ,so in VB for example we can embed this exe file so it worked,so can i do this in java?so this exe file or any other file like pictures or others can i embed them?so when i create the jar file i can move him for example to another directory and when i cliked the button i open this exe file?.
thank you very much.

Dani AI

Generated

You can bundle an .exe (or images, etc.) inside a JAR as a resource, but you cannot run a binary directly from inside the compressed JAR. The usual, reliable approach is: put the file on the classpath (resources), at runtime copy it out to a real file on disk, then launch that file with ProcessBuilder. Use Class.getResourceAsStream to read the embedded file and ProcessBuilder to run it. See the API notes for Class.getResourceAsStream and ProcessBuilder for details: Class.getResourceAsStream ProcessBuilder.

A safe pattern:

  • include the binary under your resources folder so it ends up in the JAR,
  • at runtime open it with getResourceAsStream, copy to a temp file with Files.copy, mark executable if needed, then start it with ProcessBuilder,
  • capture stdout/stderr and check exit code, and delete the temp file when finished (or call deleteOnExit).

Example extraction+run (illustrative):

try (InputStream in = MyClass.class.getResourceAsStream("/native/tool.exe")) {
    Path tmp = Files.createTempFile("tool-", ".exe");
    Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
    tmp.toFile().setExecutable(true);
    ProcessBuilder pb = new ProcessBuilder(tmp.toAbsolutePath().toString(), "arg1");
    pb.redirectErrorStream(true);
    Process p = pb.start();
    // read p.getInputStream() to see output, then p.waitFor()
    tmp.toFile().deleteOnExit();
}

Notes and gotchas (answers to gaps in the thread): ’s mention of getCodeBase applies to applets/JNLP and not to a plain desktop JAR. Make sure the embedded .exe matches the target OS/architecture and any native dependencies (VC++/.NET). Antivirus or execution policy can block temp-file execution. Verify the JAR contents with jar tf if the resource isn't found. If you want a cleaner distribution, consider shipping the native binary alongside the JAR or using a native packaging tool rather than embedding an executable inside the JAR (see the jar tool docs for packaging basics: jar tool).

Recommended Answers

All 2 Replies

don't really see how you can import a .exe in Java
will love to see the actual syntax of your import statement.
Anyhow, if you managed to realize that exploit, for it to work in a .jar you will have to jar the .exe and use methods like getCodeBase() to locate this file in your .jar

i don't mean import like import java.util.*; ,no i mean import and export when we click a right click in the project,that's why i write import pictures and others,so this is the code:

try {
		Process p = Runtime.getRuntime().exec("WindowsApplication1.exe");
		} catch (IOException e) {
			
			e.printStackTrace();
		}

so when i create the jar file,i want that "WindowsApplication1.exe" opened where i want to opened it.
thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.