0

I made two projects P1 and P2 in eclipse IDE. P2 depends on P1. I did not create any dependency between P1 and P2. I want to convert P1 to Jar and then make P2 use that Jar.

How do I do this using the commandline ? Is there a commandline inside eclipse which I could use ? If not, I will use the good old commandline.

EDIT - both p1 and p2 have a main method.

1
  • You can - and should, where appropriate - list several .jar's in your command path. DJ gave a very good explanation below. You can easily create your own .jar in Eclipse via "Project, Export; export as .jar".
    – paulsm4
    Commented Nov 26, 2012 at 2:34

3 Answers 3

2

OK -

1) Look at this tutorial:

2) Let's assume your entire program consists of one Eclipse project.

To create a .jar file, you'd generally use the Eclipse IDE: Project, <right-click>, Export, Export as .jar file

3) Let's say you want to export the .jar file manually, for whatever reason.

No problem - the tutorial above tells you how. Just:

3a) "cd" to the directory with your .class files

3b) Use Notepad (Windows) or vi (Linux) to create a "manifest.txt" file

  EXAMPLE CONTENTS: Main-Class: MyClass

3c) Use "jar" to put the manifest and the .class files into a .jar file:

  EXAMPLE: jar cvfm MyClass.jar manifest.txt *.class

4) You can create as many different .jar files from as many different projects as you want this way.

5) You would actually invoke your .jar file(s) as shown in DJ's reply.

Does that help?

2
  • Thanks. I read the tutorial and everything works. Code runs properly. Commented Nov 29, 2012 at 1:13
  • But I want to modify the command as shown below. Is that possible ? C:\Users\MyPc>cd jar cvfm C:\MyJarFiles\P2.jar C:\MyJarFiles\manifest.txt C:\eclipse\workspace\P1\bin\MyClass.class Commented Nov 29, 2012 at 1:13
2

What you need is to make sure both P1 & P2 is in the classpath. Typically from command line you execute your program with:

java -cp P1.jar:P2.jar -jar P2.jar

or

java -cp P1.jar:P2.jar x.y.z.MyProg

See the Java Tools documentation on java for more information.

2
  • I am unable to do it with these commands in cmd. My projects are located in C:\workspace. How do I modify the commands which you showed me ? I wanted to convert P1 and P2 into jars with P2 making use of P1 Jar to get the classes in P1. Do I have to convert P1 and P2 to jar in eclipse and then use your commands ? Commented Nov 26, 2012 at 3:26
  • Look at @paulsm4's answer on how to create the jar from Eclipse. Does that help?
    – DJ.
    Commented Nov 26, 2012 at 21:16
0

I suggest you find out what you really need to do. You are asking multiple questions dancing around the topic.

I assume that you need to set multiple projects and proper dependencies between them for easy set up and automatic builds.

Look at Maven or Gradle for creating descriptions on how your project is structured, how dependencies look like and how it can be built.

0

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