5

How my project structure looks like

|---> src (folder)  
         |----> Repository (folder)  
         |----> util (folder)  
         |----> config (folder)   



|---> Repository (folder)
         |---> property file 1
         |---> property file 2
         |---> property file 3

Config folder has the same structure as Repository folder.

Util folder structure is

|---> util (folder)
         |---> Main class file
         |---> Sub main class file
         |---> common function file
         |---> Report file

Now my Main class file under util folder in the default file which fetches the data from the files under Repository and config folder. It also has a link which opens the Sub main class file and uses the common function as well as Report file under the util folder itself.

I am successfully able to run the code from Eclipse but now I need to create a jar file to perform these actions. I tried to create a jar file from command prompt as well as from Eclipse, it opens the Main class file UI but unable to fetches the data from other folder files or unable to open the sub main class file.

I am pretty new to this jar thing and don't know much about it.

Suggestions ?

1
  • Although it may be useful to widen your knowledge of how Java works, you shouldn't be doing this by hand: there's plenty of tools that will almost automatically create that jar for you, like Maven or Gradle. You'll be better off if you learn to use those tools now. Commented Aug 3, 2016 at 8:07

3 Answers 3

3

You can use the following command to build the jar and specify the main class entry point (Main).

jar cfe output.jar Main src/Repository/* src/util/*.class
Sign up to request clarification or add additional context in comments.

2 Comments

I am able to create output.jar file with this command but while trying to run the same jar file from command prompt, I am getting this "no main manifest attribute, in output.jar". Do I need to perform any other action ?
you have to specify the entry point (see docs.oracle.com/javase/tutorial/deployment/jar/appman.html) with the following command line. jar cfe output.jar Main src/Repository/* src/util/*.class
1

you can write multiple files when creating the jar

jar cf Output.jar src/util/Main.class src/util/SubMain.class src/Repository/*

EDIT as per the updated requirements in comments

With this method, you'll just create a jar. Note that it is different from an executable jar. To create a executable jar, you have to specify the main class that you want to be executed when this jar will be clicked upon.

To specify that file, you've to create a MANIFEST.MF

create a file named MANIFEST.MF and put it in META-INF folder and include it while creating the jar in command line

META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.path.to.MainClass

3 Comments

I am able to create Output.jar file with this command but while trying to run the same jar file from command prompt, I am getting this "no main manifest attribute, in output.jar". Do I need to perform any other action for this ?
hi @Naseem, please see my updated answer for more details.
Thanks Raman. I created a new folder with the name META-INF above src folder and inside it, created a new file with the name MANIFEST.MF and added below line in the file META-INF/MANIFEST.MF Manifest-Version: 1.0 Main-Class: src/util/main After that I am creating the jar file with the below command jar cf Output.jar src/util/Main.java src/util/SubMain.java src/Repository/* META-INF/MANIFEST.MF But still I am getting the same error "no main manifest attribute, in Output.jar"
0

When you package files in a jar they are no longer files. They become streams/blobs. Make sure you are not loading any property files as File in your code. Use below code to load property files.

InputStream inputStreamObject = getClass().getResourceAsStream(file_name);
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStreamObject, "UTF-8"));
        StringBuilder responseStrBuilder = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.