0

If I have two projects say projectA and projectB both in different packages, the moment I add projectB to the Java Build Path from eclipse, it is allowing me to import projectB in projectA. Now is there any way I can achieve the same thing from the terminal? As far as I have searched, I did not get a proper answer.

Thanks in advance.

Neuron
  • 3,776
  • 3
  • 24
  • 44
Krishna Prashatt
  • 602
  • 6
  • 17
  • You can set the classpath, see this https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html – Alok Sinha Apr 09 '18 at 11:57

1 Answers1

1

You should build both projects and package them as JAR files. Then on the command line you include both JAR files on your classpath

For example place your JAR files in a sub-directory named lib then you can execute your application like so

java -cp ".;lib/*" my.package.MainClass

In this example I have used the Windows path separator ; inside the quotes used to define the classpath. The dot . denotes the current working directory which you may or may not need. The class my.package.MainClass is expected to exist in either the current working directory, or in any *.jar file in the lib directory.

Brad
  • 13,946
  • 10
  • 56
  • 71
  • This is probably the best way, but it should also be said that adding the directory which contains the compiled classes to the classpath also works. – kutschkem Apr 09 '18 at 11:59
  • @Brad,as per your link, the tutorial asks for a pom.xml file which i do not have. I created a Java project from eclipse and the project and its java file is stored in the workspace itself. – Krishna Prashatt Apr 09 '18 at 12:24
  • If you are not using a Maven project in Eclipse you can always use `File > Export > Jar File` for each project and then you have your JAR files. If you are learning Java I highly recommend you also learn a build tool like Maven. You can start with creating a Maven project in Eclipse and it creates everything you need for you including the pom.xml – Brad Apr 09 '18 at 12:44
  • thanks for your comment now i build both the projects and all jars are placed under a common folder. Now in terminal, from which location should I execute the above given command? – Krishna Prashatt Apr 09 '18 at 13:37
  • Thanks for guiding me like a baby.. You were really awesome and the answer worked like a king!! Thanks a ton once again.. – Krishna Prashatt Apr 09 '18 at 13:53