#I can't figure out to create a .jar file without having a ClassNotFound Exception

84 messages · Page 1 of 1 (latest)

fervent jolt
#

I have some code that is finished and I wanted to create an jar file to use it outside IDE and I looked and created an .jar file to execute my code with an MANIFEST.mf file to include the main class but even with this I still get that exception how can I do to make it works? Thanks in advance

quiet streamBOT
#

This post has been reserved for your question.

Hey @fervent jolt! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

gleaming sequoia
fervent jolt
#

what is inside the manifest file is :

#

Main-Class: Derivative.class

#

with an \n at the end

#

and the command is :

#

jar -cvfm work.jar MANIFEST.mf C:\Users\HP\Desktop\Programs\Java\Math\Calculus\cal\out\cal\*.class

#

and for running the jar file I use:

#

java -jar work.jar

gleaming sequoia
fervent jolt
#

gives me an header error if I don

#

header field error*

gleaming sequoia
#

maybe you deleted \n?

fervent jolt
#

no

gleaming sequoia
#

manifest usually has ```
Manifest-Version: 1.0
Main-Class: MainClasName

fervent jolt
#

Main-Class: Derivative

fervent jolt
#

it's suppose to only have .class file right

#

with the other classes

gleaming sequoia
#

it can contain whatever you want

#

just there should be thesame structure of the packages (directories) with including class files

#

andMETA-INF dir for maniifest file

#

and MANIFEST.MF file in it

fervent jolt
#

so I need to to the command out of the package directory

gleaming sequoia
#

sorry?

fervent jolt
#

so you see in cmd I can be in different directories and since a package is just a folder should I perform the command out of the package directory or inside of it

gleaming sequoia
#

no

#

java will be looking for the needed class inside of jar as separate classpath

#

you run the jar file correctly

#

just make sure that you have right structure

#

like ```

  • Derivative.class
  • META-INF
    -- MANIFEST.MF
fervent jolt
#

but command with jar first does I need to have the other class with the main class

gleaming sequoia
#

if main class uses another specific classes (not a part of standard jre set of classes), then yes, it has to be in the jar file as well

#

Is Derivative class in a package?

fervent jolt
#

yeah in the package cal with Render

gleaming sequoia
#

then you need to set it correctly in manifest file:```
Main-Class: Render.Derivative

#

java will be looking for the main class starting from the root of jar file

#

so if main class is in ./Render/Derivative.class, then you set the main class in manifest as I wrote above

fervent jolt
#

no Render is a class

#

cal is the package

gleaming sequoia
#

ah, so cal.Derivative

fervent jolt
#

I try

#

but still give me an header field error

#

jar cvfm MANIFEST.txt Derivative.class Render.class look for yourself

#

oh

gleaming sequoia
#

manifest.mf, not txt

#

and, since classes are in packages, you need to add them including packages

fervent jolt
#

jar cvfm work.jar MANIFEST.mf cal.Derivative.class cal.Render.class like this

#

try this : jar cvfm work.jar cal\MANIFEST.mf cal cal\Derivative.class cal\Render.class

#

but now there is no manifest attribute

gleaming sequoia
#

best would be using
jar cfe JarExample.jar cal.Derivative cal/*.class

fervent jolt
#

but there is no manifest

gleaming sequoia
#

option e automatically creates it and put as main class the second argument

#

m if you want to copy existing manifest file

#

but if only thing you need is to specify the main class, e option is better

#

since it creates manifest itself, and put as main class the class name, that you specify

fervent jolt
#

but the frame is poping out

gleaming sequoia
#

what frame?

fervent jolt
#

the frame in my program in Render.class

gleaming sequoia
#

and?

fervent jolt
#

even if there is no error if the frame doesnt appear I can't to do anything

#

with the program

gleaming sequoia
#

at the moment, does jar file work? no errors like main class not found or manifest field error etc?

fervent jolt
#

yeah

gleaming sequoia
#

good, problem is solved, now you know how to create it manually and run it

fervent jolt
#

but the frame

gleaming sequoia
#

now you should show the code

#

of your classes

fervent jolt
#
import java.lang.Math;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.*;
public class Derivative
{
    public static void main(String[] args){
        new Render();
        
      
    

    }
   
  
   
}```
#

it works

#

in my ide

#

so they should not be a problem

gleaming sequoia
#

it works for me

#

from cmd

fervent jolt
#

what did you do

gleaming sequoia
#

compiled
put in jar as I wrote above
and run

fervent jolt
#

can you write the command please

gleaming sequoia
#

when you are in project root (suppose it's test directory) you run:```
javac cal/.java
jar cfe my.jar cal.Derivative cal/
.class
java -jar my.jar

fervent jolt
#

Yes it works thank you so much

quiet streamBOT
# fervent jolt Yes it works thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

gleaming sequoia
#

np 😉