Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Folder with jars in project

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.

like image 660
Piotr Sobczyk Avatar asked Nov 11 '11 19:11

Piotr Sobczyk


People also ask

What is a jar directory?

Introduction. JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java.


4 Answers

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 !

like image 76
aleroot Avatar answered Oct 01 '22 03:10

aleroot


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

like image 30
Peter Perháč Avatar answered Oct 01 '22 03:10

Peter Perháč


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

like image 21
Suminda Sirinath S. Dharmasena Avatar answered Oct 02 '22 03:10

Suminda Sirinath S. Dharmasena


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

like image 39
Alexander Wood Avatar answered Oct 04 '22 03:10

Alexander Wood