简体   繁体   English

我无法从C ++程序运行可执行jar

[英]I am unable to run executable jar from c++ program

I used Eclipse to create a runable jar file. 我使用Eclipse创建了可运行的jar文件。 The java program writes a number to a .txt file. Java程序将数字写入.txt文件。 When I manually click on the file the program runs and creates the .txt as it should. 当我手动单击该文件时,程序将运行并按需创建.txt。 However, when I try to run the .jar from my c++ code, it seems to run but no txt file is created. 但是,当我尝试从我的C ++代码运行.jar时,它似乎可以运行,但是没有创建txt文件。 I tried this method in c++ 我在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. 我也尝试在c#中运行它,但是没有用。 Why won't my txt file successfully be created? 为什么无法成功创建我的txt文件?

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 我认为,如果您必须通过系统打开文件,则需要start因为那是您用来在cmd中启动文件的方式,因此

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

You could also try to compile the jar file to exe 您也可以尝试将jar文件编译为exe

First you have to check your java.exe in the PATH variable. 首先,您必须检查PATH变量中的java.exe。

If you don't have one, you should add your java to it. 如果没有,则应将Java添加到其中。

Here is my env on the msys2 and MinGW32. 这是我在msys2和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

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的简单Java文件,另一个是一个清单文件。

WriteFileExample.java 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 清单: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. 好的,您现在有了一个可执行的jar文件。 It should be working without any erros by following command, java -jar WeatherFinder02.jar 通过以下命令java -jar WeatherFinder02.jar,它应该可以正常工作

You will have an aa.txt file in that directory. 该目录中将有一个aa.txt文件。

Then, It is time to append the command line to the c source code. 然后,是时候将命令行附加到c源代码了。

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

    return 0;
}

I have no problem so far. 到目前为止,我没有问题。

I hope this one help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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