سؤال

أنا تنفيذية exe من خلال برنامج جافا.المسار هو ضمني في جاوة.

يجب تعبئتها بلدي exe في جرة.

ولكن أنا عالقة كما قلت اسم المسار ضمنية في ملف جافا.لذلك لست قادرة على تنفيذ جرة بلدي باعتبارها تقف وحدها البرنامج.

أي تلميحات عن التعبئة والتغليف مثل جرة أنا.ه وجود exe داخل قادرة على تشغيله باعتبارها تقف وحدها البرنامج ؟

شكرا Krisp

هل كانت مفيدة؟

المحلول

ومن الغريب علي كتابة التعليمات البرمجية مشابهة هذا الصباح...شكرا على يدفعني إلى الحصول على القيام به :-)

هذا استخراج .exe إلى ملف محلي على القرص المحلي.الملف سيتم حذفها عند برنامج جافا موجود.

تحرير:اوبس نسيت فعلا نسخ الملف...

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main
{
    public static void main(final String[] args)
        throws URISyntaxException,
               ZipException,
               IOException
    {
        final URI uri;
        final URI exe;

        uri = getJarURI();
        exe = getFile(uri, "Main.class");
        System.out.println(exe);
    }

    private static URI getJarURI()
        throws URISyntaxException
    {
        final ProtectionDomain domain;
        final CodeSource       source;
        final URL              url;
        final URI              uri;

        domain = Main.class.getProtectionDomain();
        source = domain.getCodeSource();
        url    = source.getLocation();
        uri    = url.toURI();

        return (uri);
    }

    private static URI getFile(final URI    where,
                               final String fileName)
        throws ZipException,
               IOException
    {
        final File location;
        final URI  fileURI;

        location = new File(where);

        // not in a JAR, just return the path on disk
        if(location.isDirectory())
        {
            fileURI = URI.create(where.toString() + fileName);
        }
        else
        {
            final ZipFile zipFile;

            zipFile = new ZipFile(location);

            try
            {
                fileURI = extract(zipFile, fileName);
            }
            finally
            {
                zipFile.close();
            }
        }

        return (fileURI);
    }

    private static URI extract(final ZipFile zipFile,
                               final String  fileName)
        throws IOException
    {
        final File         tempFile;
        final ZipEntry     entry;
        final InputStream  zipStream;
        OutputStream       fileStream;

        tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));
        tempFile.deleteOnExit();
        entry    = zipFile.getEntry(fileName);

        if(entry == null)
        {
            throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
        }

        zipStream  = zipFile.getInputStream(entry);
        fileStream = null;

        try
        {
            final byte[] buf;
            int          i;

            fileStream = new FileOutputStream(tempFile);
            buf        = new byte[1024];
            i          = 0;

            while((i = zipStream.read(buf)) != -1)
            {
                fileStream.write(buf, 0, i);
            }
        }
        finally
        {
            close(zipStream);
            close(fileStream);
        }

        return (tempFile.toURI());
    }

    private static void close(final Closeable stream)
    {
        if(stream != null)
        {
            try
            {
                stream.close();
            }
            catch(final IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

نصائح أخرى

نظام التشغيل لا يهتم أو معرفة .ملف jar, لذلك عليك أن فك .exe ملف لبعض المؤقتة الموقع قبل تنفيذ ذلك.

//gets program.exe from inside the JAR file as an input stream
InputStream is = getClass().getResource("program.exe").openStream();
//sets the output stream to a system folder
OutputStream os = new FileOutputStream("program.exe");

//2048 here is just my preference
byte[] b = new byte[2048];
int length;

while ((length = is.read(b)) != -1) {
    os.write(b, 0, length);
}

is.close();
os.close();

في حين أن المستخدمين الآخرين قد أجبت على السؤال بشكل صحيح ، واستخراج ثم تشغيل تنظيف.نقطة أخرى للنظر هو الذهاب أصلي تماما.

كنت بالفعل باستخدام اللغة الأم الثنائية لتحقيق مهمة محددة.لماذا لا أيضا إنشاء المثبت الأصلي الذي سيتم تثبيت التطبيق الخاص بك وتثبيت الثنائية إلى نظام التشغيل موقع معين (ملفات البرنامج على Win32) وخلق مناسبة اختصارات.

هذه طريقة التطبيق الخاص بك سوف يشعر أكثر مواطن يعني أنك لا تحتاج إلى كتابة أو إدارة رمز للالتفاف على هذه الحقيقة.في هذه اللحظة جرة يشبه عبر منصة قطعة من التعليمات البرمجية (جرة يعمل في أي مكان أليس كذلك؟) ولكن حزم الأصلي الثنائية التي لا تعمل في كل مكان.هذا يبدو وكأنه تناقض.

القائمين يمكنني أن أوصي نولسوفت تركيب نظام (NSIS) كما لديهم العديد من الدروس الممتازة و نماذج التعليمات البرمجية للتعلم من.

استخدام

getClass().getResource(what).openStream()

ونسخ إلى آخر ملف في القرص.

هل يمكن كتابة برنامج جافا لتشغيل exe استخدام وقت التشغيل.exec().يمكنك ثم تعيين "الرئيسي من الدرجة" سمة جرة أن يكون بهذه الدرجة قاذفة.يمكن للمستخدمين ثم تشغيل جرة الخاص بك و سوف تشغيل exe.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top