-2

I want to know how to export a jar file from within java code. Look at the following situation:

I have some .java files including one main class. I want to know how to pack those files and convert them to an executable jar file. I don't want to do this the traditional way by exporting the project to a jar file using eclipse built in functionality (I know how to do that). I want to know how to export the .java files and the required libraries to a jar file using java code.

Here is my ideal result: I run my own jar exporting program and it asks me to select some files. When I do select and hit 'export', it packs those files in an executable jar file. Thanks in advance and peace out.

Ali Aman
  • 87
  • 7

1 Answers1

0

Maybe you could try running cmd commands for creating JAR file using JAVA code.

You can create jar File in Command Prompt following these steps:

 - Start Command Prompt.

 - Navigate to the folder that holds your class files:

    C:\>cd \mywork

 - Set path to include JDK’s bin.  For example:

    C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%

 - Compile your class(es):

    C:\mywork> javac *.java

 - Create a manifest file

   manifest.txt with, for example, this content:

        Manifest-Version: 1.0
        Created-By: 1.8.0_171 (Oracle Corporation) #your jdk version
        Main-Class: mainPackage.MainClass

 - Create the jar file:

    C:\mywork> jar cvfm Craps.jar manifest.txt *.class

To see how can we run these commands within JAVA code, follow this link:

Run cmd commands through Java