简体   繁体   中英

I am unable to run executable jar from c++ program

I used Eclipse to create a runable jar file. The java program writes a number to a .txt file. When I manually click on the file the program runs and creates the .txt as it should. However, when I try to run the .jar from my c++ code, it seems to run but no txt file is created. I tried this method in c++

system("C:\Users\anon\Downloads\KidCod\KidCod\Java\WeatherFinder02.jar");

However, it didn't work. So I tried this method:

ShellExecute(NULL, "open", "C:\Users\anon\Downloads\KidCod\KidCod\Java\WeatherFinder02.jar.c", NULL, NULL, SW_SHOWDEFAULT);

But it still doesn't work. I don't get any errors or anything like that. I tried to run it in c# as well but it didn't work. Why won't my txt file successfully be created?

I think if you have to open a file through system you need start because that is what you use to start a file in cmd so

system("start C:\\Users\\anon\\Downloads\\KidCod\\KidCod\\Java\\WeatherFinder02.jar");

You could also try to compile the jar file to exe

First you have to check your java.exe in the PATH variable.

If you don't have one, you should add your java to it.

Here is my env on the msys2 and MinGW32.

export JAVA_HOME=/D/DEV/SDK/JDK/jdk1.8.0_152
export PATH=$JAVA_HOME/bin:$PATH

Second, see if your java is working with -version option as following

java -version

java version "1.8.0_152-ea"
Java(TM) SE Runtime Environment (build 1.8.0_152-ea-b02)
Java HotSpot(TM) Client VM (build 25.152-b02, mixed mode)

Then use the command in the source code.

#include <stdlib.h>
#include <string.h>
#include <string.h>

int main()
{
    system("java -version");

    return 0;
}

The output should be the same with that of command line before.

To solve your problem, i create two files, one is a simple java file called WriteFileExample.java and the other is one manifest file.

WriteFileExample.java

package com.tobee.test;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class WriteFileExample {
    public static void main(String[] args)
    {
        PrintWriter out = null;

        try {
            out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("aa.txt")));
            out.print(1);
            out.print(3);
            out.print(4);
            out.print(7);
            out.print(9);
            out.println();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally
        {
            if(out != null)
            {
                out.flush();
                out.close();
            }

        }
    }
}

manifest : MANIFEST.MF

Main-Class: com.tobee.WriteFileExample

I compile it and make it jar with the manifest file by,

javac -d . WriteFileExample.java
jar -cvmf MANIFEST.MF WeatherFinder02.jar com/tobee/test/WriteFileExample.class

Finaaly, you have a executable jar file now. It should be working without any erros by following command, java -jar WeatherFinder02.jar

You will have an aa.txt file in that directory.

Then, It is time to append the command line to the c source code.

int main()
{
    system("java -version");
    system("java -jar WeatherFinder02.jar");

    return 0;
}

I have no problem so far.

I hope this one help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM