How to add a directory to the eclipse class path?

I am trying to run an existing eclipse project created by another person.
After importing it into eclipse and trying to run As-> Java Application, it fails because it cannot find the .properties file in bin / resources

I printed the eclipse classpath using

logger.info(System.getProperty("java.class.path")); 

and of course, it includes bin and all lib / *. jars but not bin / resources. Copying the .properties file to bin does the work of the program, but I would like to understand how to add the directory to the eclipse class path.

I tried a few things, none of which worked

 export CLASSPATH=$CLASSPATH:/home/me/programdir/bin/resources 

This did not work. I understand that this is not a desirable way to approach the problem, but I thought it would fix the problem (I have more windows than the linux background, so maybe I miss some of the nuances of system variables in Linux)

The next time I tried changing the arguments of a virtual machine in the Run-> Configurations dialog in Eclipse

 -classpath "/home/me/programdir/bin/resources" 

I was also unlucky that I was confused, since I was sure that this would work and seems like a reasonable solution for a specific program that requires an additional folder added to the classpath.

Next, I tried to modify build.xml directly. I found the part that defines the classpath and added my own line for bin / resources, as follows:

 <path id="classpath"> <fileset dir="./bin/resources" includes="**/*.properties"/> <fileset dir="./lib" includes="**/*.jar" /> </path> 

This, too, was unsuccessful. This puzzled me even more, so I commented on the whole path element, and the class path printed by the log remained unchanged, so it is obvious that any use of eclipse in the class, of course, was not that. It seemed to me the best solution if it worked: the build.xml file could be checked with the right add-ons to prevent future users from running into a problem.

Next, I tried to apply the IDE approach. Run-> Configurations-> Classpath-> User Entries-> Advanced and just added the bin / resources folder. This worked perfectly, the program finds the properties file, everything is in order. However, I am unhappy that my previous efforts failed, and I did not understand why. It seems that each of them should have worked.

In addition, I want me to fix this problem so that it is fixed by the code that I check so that subsequent users do not have to go through the same steps. My solution, therefore, is not very satisfactory, since I am not sure that I changed the actual code fragment and therefore cannot check if the "fix" is checked.

How do you find the actual definition that eclipse uses for its classpath? I thought this would be the classpath definition for build.xml, but it seems like this is not at all.

+6
source share
3 answers

In Eclipse, there is a path to the assembly class and a path to the runtime. There is also an assembly output location, which is bin by default. You do not want to add resources directly to bin , because Eclipse can delete its contents when performing a clean build. What you need to do is add a folder to the resources project to accommodate any non-Java files that you want to include in the output of the assembly.

To include the contents of this resources folder in the output of the assembly ( bin ), right-click the project and select Properties. In the project properties, select the Java Build Path section, and then the Source tab.

enter image description here

Use the Add Folder ... button to select the resources folder from your project, and then OK to save the changes. At this point, Eclipse will automatically copy everything from resources to bin when it is created.

+14
source

This is for the maven project:

  • Right click on the project
  • click on startup settings
  • click on the class path tab (Oxygen Eclipse)
  • click on user entries
  • click "Advanced"
  • The first default radio selection should be "Add Folders"
  • click OK
+2
source

Right-click the project name in Package Explorer , select Properties , select Java Build Path on the left, select Source on the right, click Add Folder , browse the project directories to select the resource folder or whatever you need to add to the eclipse class path, click OK . click OK again. Done.

0
source

Source: https://habr.com/ru/post/973427/


All Articles