如何向classpath添加资源 [英] How to add resources to classpath

查看:65
本文介绍了如何向classpath添加资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文件夹(例如包含arts的资源文件夹)添加到netbeans项目的类路径中?我设法通过编辑NB生成的项目jar文件(即MANIFEST.MF文件+手动复制资源)手动完成,但是应该有一种方法告诉netbeans也要记住资源,不是吗? / p>

文件夹结构如下所示:

  / project / art / 
/ project / dist / lib /
/ project / dist / art /
/project/dist/project.jar
/ project / lib /
/ project / src /

我不想将艺术品打包到罐子里,因为我喜欢艺术品易于交换。如果我将art文件夹添加到src文件夹,那么NB编译正常,但艺术资源最终会在jar中。



将art文件夹添加到netbeans项目库(属性 - >库 - >添加JAR /文件夹)似乎不起作用,因为那时我最终得到一个错误'... \ project.\\art是一个目录或无法读取。不复制库。'这反过来又阻止了真正的库文件夹被复制。



任何想法?



最好的问候
Chris



2基于来自gpeche的评论进行观察:
a)而不是在运行中指定其他资源文件夹选项卡比项目属性的编译选项卡 - >库在Netbeans中似乎没有太大的区别(我目前使用的是6.9.1)。输出(以及因此错误)保持不变,根本不会复制任何内容:

 创建的目录:C:\用户\ ChrisHesi \Desktop; \\词汇\VocabularyTrainer \ dist 
C:\ Users \Chrisi \Desktop\vocabulary \VocabularyTrainer \ art是一个目录或无法读取。不复制库。
不复制库。
建筑罐子:C:\ Users \Chrisi \Desktop\vocabulary\VocabularyTrainer\dist\VocabularyTrainer.jar

另一个有趣的方面是,在库面板的帮助菜单中,没有明确提及有关文件夹的库。是否有可能,Netbeans中的按钮被错误地命名,只允许真正的jar?



b)将资源文件夹添加到Libraries列表中确实有但是,要向MANIFEST.MF添加另一个条目。虽然 - 这是较小的问题 - 类路径条目似乎总是期望资源文件夹是库文件夹的子文件夹(例如lib / arts),主要问题是似乎缺少斜线。
如前所述,MANIFEST.MF中的NB生成条目看起来像这个lib / arts(这对我不起作用),而(手动设置)lib / arts /会这样做吗?!



我使用文件夹中的资源的方式是这样的:

  URL resource = getClass()。getResource(/ gui / refresh.png); 
ImageIcon tmp = new ImageIcon(resource);

修改:



基于Tushars的评论和这篇文章我发现以下解决方案在功能和舒适度之间是可接受的权衡。



我从自动生成的'build-impl中覆盖ANT目标。 xml'文件,它在Netbeans项目的基本'build.xml'文件中的MANIFEST.MF文件中创建Class-Path。转到'build.xml'文件的代码如下所示:

 < property name =art.classpath value =art //> 

< target name = - post-jar>
< mkdir dir =$ {dist.dir} / art/>
< copy todir =$ {dist.dir} / art>
< fileset dir =$ {basedir} / art>
<! - < exclude name =** /!source / **/>如果你想排除某些东西...... - >
< / fileset>
< / copy>
< / target>

< target name = - init-macrodef-copylibs>
< macrodef name =copylibsuri =http://www.netbeans.org/ns/j2se-project/3>
< element name =customizeoptional =true/>
< sequential>
< property location =$ {build.classes.dir}name =build.classes.dir.resolved/>
< pathconvert property =run.classpath.without.build.classes.dir>
< path path =$ {run.classpath}/>
< map from =$ {build.classes.dir.resolved}to =/>
< / pathconvert>
< pathconvert pathsep =property =jar.classpath>
< path path =$ {run.classpath.without.build.classes.dir}/>
< chainedmapper>
< flattenmapper />
< globmapper from =*to =lib / */>
< / chainedmapper>
< / pathconvert>
< taskdef classname =org.netbeans.modules.java.j2seproject.copylibstask.CopyLibsclasspath =$ {libs.CopyLibs.classpath}name =copylibs/>
< copylibs compress =$ {jar.compress}index =$ {jar.index}jarfile =$ {dist.jar}manifest =$ {manifest.file}runtimeclasspath = $ {run.classpath.without.build.classes.dir}>
< fileset dir =$ {build.classes.dir}/>
< manifest>
< attribute name =Class-Pathvalue =$ {jar.classpath} $ {art.classpath}/>
< customize />
< / manifest>
< / copylibs>
< / sequential>
< / macrodef>
< / target>

权衡对于Netbeans的开发,我仍然需要添加资源文件夹(例如'art' )到库列表,使项目在Netbeans中运行。这将导致MANIFEST.MF文件('lib / art')中的其他条目以及库不会自动复制到'dist'文件夹的效果,并显示消息

  ... \ art是一个目录或无法读取。不复制库。 
不复制库。

这个行为是 - afaik--意图(强迫所有东西捆绑在一个罐子里),甚至虽然有关于它的讨论正在进行中。要制作一个真正的分发包,我必须从NB中的库列表中取出资源文件夹并重建。



有关更简化设置的想法当然,任何权衡都是受欢迎的。 :)

解决方案


  1. 将资源文件夹添加到类路径:



    当您清理 - 并且 - 建立基于NetBeans Ant的项目时,它会在项目的根目录中创建一个manifest.mf文件。该文件也包含在JAR文件中。修改此文件并添加如下条目:

     清单 - 版本:1.0 
    X -COMMENT:Main-Class将自动添加
    Class-Path:arts /

    在课程道路上艺术之后,斜线很重要。


  2. 在发行版中包含艺术资源文件夹



    在构建后复制dist文件夹中的此文件夹,或添加ANT目标以复制dist文件夹中所需的资源。



    在build.xml文件中添加如下目标:

     < target name = -post-罐子> 
    < mkdir dir =$ {dist.dir} / resources/>
    < copy todir =$ {dist.dir} / resources>
    < fileset dir =$ {basedir} / resources/>
    < / copy>
    < / target>


  3. 访问此类资源的代码:



    访问此类资源文件所需的代码如下:(这在设计时不起作用,但肯定来自JAR文件)

      //注意相对路径
    URL resource = ClassLoader.getSystemResource(gui / refresh.png);
    ImageIcon tmp = new ImageIcon(resource);

    注意:位于项目根目录中的文件manifest.mf和build.xml是可访问的从NetBeans IDE中的文件面板



How do you add a folder (e.g. a resource folder containing arts) to the classpath of a netbeans project? I managed to do that manually via editing the NB generated jar file of the project (that is its MANIFEST.MF file + copying the resources manually), but there should be a way to tell netbeans as well to mind the resources, no?

The folder structure looks like this:

/project/art/
/project/dist/lib/
/project/dist/art/
/project/dist/project.jar
/project/lib/
/project/src/

I don't want to package the art into the jar because I'd like the art to be easily exchangeable. If I add the art folder to the src folder then NB compiles fine, but the art resources end up in the jar.

Adding the art folder to the netbeans project libraries (Properties -> Libraries -> Add JAR/Folder) seemed not to work, because then I ended up with an error '...\project\art is a directory or can't be read. Not copying the libraries.' which in turn prevents even the real libraries folder from being copied.

Any ideas?

Best regards Chris

2 Observations made, based on the comments from gpeche: a) Rather specifying the additional resources folder in the "Run" tab than in the "Compile" tab of the project properties -> Libraries doesn't seem to make a lot of difference in Netbeans (I'm currently using 6.9.1). The output (and thus error) stays the same, that is nothing gets copied at all:

Created dir: C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\dist
C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\art is a directory or can't be read. Not copying the libraries.
Not copying the libraries.
Building jar: C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\dist\VocabularyTrainer.jar

Another interesting aspect is that in the help menu of the Libraries panel nothing is explicitly mentioned regarding folders as libraries. Could it be possible, that the button in Netbeans is falsely named, that is only real jar's are allowed?

b) Adding the resources folder to the Libraries list does have the impact though, to add another entry to the MANIFEST.MF. While - and that's the smaller issue - the classpath entry seems to be always expecting the resource folder to be a subfolder of the library folder (e.g. "lib/arts") the major problem is that there seems to be a slash missing. As mentioned the NB generated entry in the MANIFEST.MF will look like this "lib/arts" (which does not work for me), while (manually set) "lib/arts/" does?!

The way I use resources from the folder is something like this:

URL resource = getClass().getResource("/gui/refresh.png");
ImageIcon tmp = new ImageIcon(resource);

Edit:

Based on Tushars comment and this posting I found the following solution to be an acceptable tradeoff between functionality and comfort.

I override the ANT target from the auto generated 'build-impl.xml' file which creates the Class-Path in the MANIFEST.MF file in the basic 'build.xml' file of the Netbeans project. The code which goes to the 'build.xml' file looks like this:

  <property name="art.classpath" value="art/" />

  <target name="-post-jar">
    <mkdir dir="${dist.dir}/art"/>
    <copy todir="${dist.dir}/art">
      <fileset dir="${basedir}/art">
        <!-- <exclude name="**/!source/**"/> if you want to exclude something... -->
      </fileset>
    </copy>
  </target>

  <target name="-init-macrodef-copylibs">
    <macrodef name="copylibs" uri="http://www.netbeans.org/ns/j2se-project/3">
      <element name="customize" optional="true"/>
      <sequential>
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
          <path path="${run.classpath}"/>
          <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <pathconvert pathsep=" " property="jar.classpath">
          <path path="${run.classpath.without.build.classes.dir}"/>
          <chainedmapper>
            <flattenmapper/>
            <globmapper from="*" to="lib/*"/>
          </chainedmapper>
        </pathconvert>
        <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
        <copylibs compress="${jar.compress}" index="${jar.index}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
          <fileset dir="${build.classes.dir}"/>
          <manifest>
            <attribute name="Class-Path" value="${jar.classpath} ${art.classpath}"/>
            <customize/>
          </manifest>
        </copylibs>
      </sequential>
    </macrodef>
  </target>

The tradeoff is that for development in Netbeans I still have to add the resource folder (e.g. 'art') to the libraries list to make the project run in Netbeans. This will cause an additional entry in the MANIFEST.MF file ('lib/art') along with the effect that the libraries will not get automatically copied to the 'dist' folder, with the message

...\art is a directory or can't be read. Not copying the libraries.
Not copying the libraries.

This behavor is - afaik - intended (to force everything to be bundled up in a jar), even though there are discussions about it going on. To make a real distribution bundle I'd have to take away the resource folder(s) from the library list in NB and rebuild.

Ideas about a more streamlined setup without any tradeoffs are of course still welcome. :)

解决方案

  1. Adding resource folder to classpath:

    When you Clean-&-Build a NetBeans Ant Based Project it creates a manifest.mf file in the root directory of the project. This file gets included in the JAR file also. Modify this file and add entry like follows:

    Manifest-Version: 1.0
    X-COMMENT: Main-Class will be added automatically by build
    Class-Path: arts/  
    

    slash is important after arts in the class path.

  2. Including the arts resource folder in the distribution

    Either copy this folder in the dist folder after the build or add a ANT target to copy the required resources in the dist folder.

    Add the target like as follows in the build.xml file:

    <target name="-post-jar">
         <mkdir dir="${dist.dir}/resources"/>
         <copy todir="${dist.dir}/resources">
             <fileset dir="${basedir}/resources" />
         </copy>
     </target>
    

  3. Code to access such resources:

    The code needed to access such resource files shall be as follows: (This will not work in design time but surely from the JAR file)

    // pay attention to relative path
    URL resource = ClassLoader.getSystemResource("gui/refresh.png"); 
    ImageIcon tmp = new ImageIcon(resource);
    

    NOTE: The files manifest.mf and build.xml located in the root directory of the project are accessible from the Files Panel in NetBeans IDE

这篇关于如何向classpath添加资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆