Folder with jars in project
Asked Answered
A

5

14

When I work on small desktop projects I used to create lib folder in my project's root where I keep all project's jar dependencies. Then I use Configure Build Path -> Libraries -> Add JARs... to manually add all jars from this folder to buildpath/classpath. And because Add JARs... (unlike Add external JARs) uses relative paths, the project is portable, what is important for me.

The problem is that each time I add or remove a jar from my lib folder I need to manually add/remove this jar in project buildpath settings (and of course I often forget to do so).

Is there a way to just inform Eclipse that "This is a folder where I keep all of my jars. Please, add all the jars from there automatically to buildpath/classpath"? I tried to treat this folder as a class folder (Add class folder...) but it doesn't work that way :(.

P.S. I know about Maven and Eclipse-Maven integration but I want to keep my small project's simple (Maven integration is sometimes frustrating so I prefer to avoid it in these projects), so please don't suggest this in answer. Also as I mentioned, these are desktop projects, so there is no WEB-INF/lib folder in my project that is usually automatically handled by Java EE plugins.

Ankh answered 11/11, 2011 at 19:26 Comment(2)
totally agree, maven can be such an overkill for small projects. It's a life saver on anything bigger than basic tinkering, but the hassle...omgPizor
This look a lot like a duplicate of this stackoverflow question: #484063Brocket
C
4

you can try with a classpath container, take a look here for an example .

Take a look also at the Apache IvyDE classpath container .

However adding a new library to the classpath is simple and quick as :

Right click on it ---> Build Path ---> Add To Build Path

EDIT

This lightweight plugin should do exactly what you want !

Creditor answered 2/12, 2011 at 21:16 Comment(5)
It seems there is no way to reach what I need in easy way. However answer pointing to classpath containers is the closest to what I asked for. The problem is that it seems like a big effort to implement a classpath container. Are there any more pre-implemented classpath containers like IvyDE classpath container? Maybe one of them would solve my problem out of the box?Ankh
Great, it's in fact just what I need! You definitely deserved for a bounty!Ankh
The plugin pointed by aleroot works great. You add directeory-based classpath in Eclipse build path settings -> Libraries -> Add Library... -> Directory classpath container. The only concern may be that if you open the project on other Eclipse instance without plugin installed, and add new files to the directory, they are are no longer automatically added to classpath (but already added jars remain on classpath), so plugin needs to be installed in Eclipse to make use of classpath container it generated for project. But it's not a problem for me.Ankh
404 link and no "Build Path" from the navigator windowSubcontract
@Subcontract the answer was written 5 years ago and I don't check every day if the links are still working ... Anyway the Build path is not present in Project Navigator then you have to use Package Explorer view ...Creditor
W
2

I am not too sure, but can't you have wildcards in your classpath? That way you could just edit your .classpath file for that Eclipse project and use * within a particular folder... I have not tried, i'm in a rush but that's my idea... don't know if works

EDIT here is something that you could find useful: How to use a wildcard in the classpath to add multiple jars?

Basically, just edit your .classpath file, which is where Eclipse stores the classpath settings for a project

Weichsel answered 11/11, 2011 at 19:59 Comment(3)
Thanks, it seems like a great idea. Unfortunately, Eclipse doesn't seem to understand this notation. I added <classpathentry kind="lib" path="lib/*"/> entry to my .classpath file but I have compilation errors because Eclipse didn't add jar's from lib/* to buildpath (possibly it will be correctly resolved as classpath). Anyways, nice to know about classpath wildcard notation.Ankh
@PiotrSobczyk try this: <classpathentry kind="lib" path="lib/*.jar"/> or <classpathentry kind="lib" path="lib/**/*.jar"/>.Kerstin
@HarryJoy It still doesn't work. Eclipse just doesn't interpret *s. It treats them verbosim and tries to find jar named *.jar in directory named lib/*.Ankh
M
1

I think the best is to use Gradle. This does not have the frustration of Maven with Eclipse. If you use STS it comes with Gradle pre-bundled.

See the link

Melvinmelvina answered 29/11, 2011 at 19:11 Comment(1)
Also you can try what @harry Joy suggests.Melvinmelvina
L
1

So yeah I did this before: Use Apache Ant and specify an ant configuration that suits your build path and eclipse should be able to use it with the use from existing ant build option. Here is an example ant file you might have:

<?xml version="1.0"?>

<project name="Demo Project" basedir="." default="package">
  <!-- ================================================================= -->
  <!-- C O N F I G U R A T I O N                                         -->
  <!-- ================================================================= -->
  <!--
    Access the environment properties
    -->
  <property environment="env" />
  <!--
  TODO: Access the environment properties with a prefix of "env".
  -->
  <!--  
  Additional 3rd-party tools
   -->
  <property name="ant.home"      value="${env.ANT_HOME}"/>
  <property name="junit.home"    value="${env.JUNIT_HOME}"/>
  <property name="jmock.home"    value="${env.JMOCK_HOME}"/>
  <!--
  Project folders
  -->
  <property name="project.home"  value="${basedir}" />
  <property name="bin.dir"       value="${project.home}/bin" />
  <property name="dist.dir"      value="${project.home}/dist" />
  <property name="dist.file"     value="${dist.dir}/lab03.jar" />
  <property name="col.file"      value="${dist.dir}/lab03-col.jar" />
  <property name="src.dir"       value="${project.home}/src" />
  <property name="lib.dir"       value="${project.home}/lib" />

  <!--
  TODO: Define the classpath to be used during compilation. This should
  consist of all of the JAR files in the ${lib.dir} folder.
  -->
  <path id="project.class.path">
    <path location="${dist.file}" />
    <path location="${bin.dir}" />
    <fileset dir="${junit.home}">
    <include name="junit-4.7.jar"/>
    </fileset>
    <fileset dir="${jmock.home}">
    <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${ant.home}/lib">
        <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${lib.dir}">
        <include name="**/*.jar"/>
    </fileset>  
  </path>

  <!--
  TODO: Define the classpath to be used during execution. This should
  consist of all of the JAR files in the ${lib.dir} folder as well as
  ${dist.file}.
  -->
  <path id="execution.class.path">
       <path location="${bin.dir}" />
       <path location="${bin.dir}/MyPath1/MyPath" />
       <path location="${bin.dir}/MyPath1/MyPath/impl" />
       <fileset dir="${lib.dir}">
          <include name="**/*.jar"/>
       </fileset>   
  </path>

  <!-- ================================================================= -->
  <!-- C L E A N                                                         -->
  <!-- ================================================================= -->
  <target name="clean"
          description="Clean all build products">
    <delete dir="${bin.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- C O M P I L E                                                     -->
  <!-- ================================================================= -->
  <target name="compile" 
          depends="clean,init"
            description="Compiles the application code">
    <!--
    TODO: Add the javac task. It should compile everything in ${src.dir}
    and place the output in ${bin.dir}. The classpath should refer to the
    "project.class.path" defined above.
    -->
    <javac srcdir="${src.dir}"
               destdir="${bin.dir}">
       <classpath refid="project.class.path" />
    </javac>
  </target>

  <!-- ================================================================= -->
  <!-- E N V                                                             -->
  <!-- ================================================================= -->
  <target name="env"
            description="Displays information about the build">
    <echo message="src.dir..........${src.dir}" />
    <echo message="lib.dir..........${lib.dir}" />
    <echo message="bin.dir..........${bin.dir}" />
    <echo message="dist.dir.........${dist.dir}" />
    <echo message="dist.file........${dist.file}" />
    <echo message="col.file.........${col.file}" />
    <echo message="reports.dir......${reports.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- I N I T                                                           -->
  <!-- ================================================================= -->
  <target name="init" 
          depends="env"
          description="Initializes the environment">
    <mkdir dir="${bin.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- P A C K A G E                                                     -->
  <!-- ================================================================= -->
  <target name="package" 
          depends="compile"
            description="Creates the application distribution file">
    <!--
    TODO: Create a JAR file. The target JAR should be ${dist.file} and it
    should contain everything from ${bin.dir}.
    -->
    <jar destfile="${dist.file}"
           basedir="${bin.dir}"
           excludes="**/*Test*.class"
    />
  </target>

  <!-- ================================================================= -->
  <!-- P A C K A G E - C O L                                             -->
  <!-- ================================================================= -->
  <target name="package-col" 
          depends="compile"
            description="Creates the file to be submitted to COL.">
    <jar destfile="${col.file}">
      <fileset dir="${project.home}"
               includes="src/**/*.java" />
      <fileset dir="${project.home}"
               includes="lib/**/*.jar" />
      <fileset dir="${project.home}"
               includes="build.xml" />
    </jar>
  </target>

  <!-- ================================================================= -->
  <!-- R U N                                                             -->
  <!-- ================================================================= -->
  <target name="run" 
          depends="package"
            description="Executes the test file">
    <java classname="MyPath1.MyPath.FileScanner">
      <classpath refid="execution.class.path" />
      <arg value="file:///" />
    </java>
  </target>
</project>

AND Here is a link with someone with a similair problem using ant to solve his classpath problems. Ant is portable so it can actually be set up anywhere and you can also use global variables to keep all systems consistent or just use relative paths. And there is also an eclipse ant plugin

Lidia answered 2/12, 2011 at 23:17 Comment(0)
D
-1

just try including

<classpathentry kind="lib" path="lib/spring/4.2.1" including="*.jar"/>
Differentiation answered 10/9, 2015 at 13:32 Comment(1)
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".Mongolia

© 2022 - 2024 — McMap. All rights reserved.