简体   繁体   中英

How to store paths in configuration file in netbeans?

I have a java project in netBeans that deals with some paths in order to be able to work. It is headache to change the paths every time that you are run it on other machine. Thus, I am wondering if there is a way that help me to come over this issue. One suggested to me to use configuration file, but I don't have any idea about how to do it. So, kindly could you help me in this, please?

You should have the Ant target run in your build.xml file. In most cases this target contains only one task - the java task. You can add arg elements to this task. One of possible types of these elements is file . So, your target will look like this:

<target name="run" depends="build">
  <java classpath="${basedir}" classname="..." fork="yes">
    <jvmarg value="-enableassertions"></jvmarg>
    <arg file="abc.txt"/>
    <arg file="def.txt"/>
  </java>
</target>

The Java interpreter will look for files abc.txt and def.txt in the base directory of your project. So, if you run the NetBeans on different machines, then it will be enough to have your data files in this directory. Of course, it's not only possibility - the Ant build.xml file is flexible enough to define any location you want.

As for more info about Ant - http://ant.apache.org/manual/using.html#arg

ADDITION:

The filenames, defined via arg elements in the build.xml file, will be accessible from Java code via the main function argument array. So, the program:

public static void main(String ARG[])
{
  for (String s: ARG) System.out.println(s);
}

will print:

<your absolute project directory>/abc.txt
<your absolute project directory>/def.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM