1

I had created a jar file with three classes using this cmd line:

C:\...\db>jar cvf views.jar Line.java Points.java Shapes.java

I can add the jar file to IDE but I can't import it to the code.

Another thing is the classes in the jar file are xxx.java, but when looking in another jar file i noticed that the classes are xxx.class

I don't know if that is the problem or not.

5 Answers 5

3

Jar is nothing but archiving (zipping)

You are clearly zipping the .java files to your jar file.

First Compile your .java files using javac

than issue your jar command on the generated .class files

Refer How to Create Jar

3
  • to compile the .java i need a main method(correct me if i'am wrong), and i don't have a main method, there is no need to it. but if there is no way to compile it without the main method i'll try to fix it. sorry if am wrong but am new on java :)
    – IBRA
    Jan 17, 2013 at 13:18
  • to execute .class file you need main() in corresponding .java. not for compiling. any valida .java file can be compiled note that not all .java files will have main method. Jan 17, 2013 at 13:20
  • thanks, it's now more clear to me. i'll see the link you referred to me.
    – IBRA
    Jan 17, 2013 at 13:27
2

You should refer to .class files when creating the JAR file instead of the source .java file.

Here you can find a tutorial about JAR creating from Oracle official site.

0

jar is an archive tool which is just packing your compiled java files. This means that you should compile those .java files first, and than add generated .class files in jar.

0

I would normally expect a .jar file to contain classes rather than .java files (or at least the source could be alongside the classes)

You can zip anything into a .jar file (it's a .zip by another name - see here for more info) but I suspect the IDE is expecting .class files. Note that my IDE (Intellij) allows me to specify a jar/zip containing source, but that's usually alongside a jar containing the compiled code.

0

You package java source files instead of compiled class files in the jar. If you want you jar to be used in IDE you should package class files. Something like

C:\...\db>javac Line.java Points.java Shapes.java

and then

C:\...\db>jar cvf views.jar Line.class Points.class Shapes.class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.