How to auto configure war file in eclipse - java

I have a problem with my war.xml file in that it isn't picking up my class files correctly for spring.
The line that is wrong is:
<dir sourceOnDisk="C:\myApp\src\main\java" targetInArchive="/WEB-INF/classes"/>
I need:
<dir sourceOnDisk="C:\myApp\bin" targetInArchive="/WEB-INF/classes"/>
Now, I can change the file manually (from src\main\java to bin), and it works correctly for a while, but every now and again something decides my war.xml file needs to be rebuilt, and things then break again.
I added my application by dragging my project from eclipse's Project Explorer tab to my liberty profile server instance within eclipse's Servers tab.
Where is the menu option within eclipse to change this?

To automatically map your bin folder to the WEB-INF/classes folder, select your project on the Project Explorer, right click it and select Java Build Path. In this property page on the Source tab, you can add, edit or remove the source folder, and also modify the default output folder. In this case you may only need to modify the output folder, to something similar to (project-name)/bin
Regarding the *war.xml file located at (server-dir)/apps, this will be refreshed each time you modify your project in eclipse, if when doing it, you have your server started and with automatic publish enabled. Usually you don't modify this file yourself, but you can read more about it here: http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.wlp.nd.doc/ae/rwlp_loose_applications.html?cp=SSAW57_8.5.5%2F1-14-1-10

Turns out the correct place to change things is in the Deployment Assembly option when you right click on your project...

Related

How do I add a directory to the eclipse classpath?

I'm attempting to run an already existing eclipse project created by another person.
After importing it to eclipse, and attempting to Run As->Java Application, it fails because it cannot find a .properties file in bin/resources
I printed out the classpath eclipse was using
logger.info(System.getProperty("java.class.path"));
and sure enough, it includes bin, and all the lib/*.jars, but not bin/resources. Copying the .properties file into bin makes the program work, but I wanted to understand how to add a directory to the eclipse classpath.
I tried several things, none of which worked
export CLASSPATH=$CLASSPATH:/home/me/programdir/bin/resources
This didn't work. I understand it's not a desirable way to approach the issue, but I had thought it would fix the problem (I have more of a windows than linux background so perhaps I am missing some nuances of system variables in linux)
Next up, I tried modifying the VM arguments in the Run->Configurations dialog in Eclipse
-classpath "/home/me/programdir/bin/resources"
No luck here either, which confused me, as I was sure it would work and seemed like a reasonable solution to a specific program needing one additional folder added to the classpath.
Next I tried modifying 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 perplexed me even more, so I commented out the entire path element, and the classpath printed out by the logger was unchanged, so it is apparent that whatever classpath eclipse was using, it certainly wasn't this one. This seemed to me the best solution, had it worked: the build.xml file could be checked in with the correct additions to prevent future users from experiencing the problem.
Next I tried the IDE approach. Run->Configurations->Classpath-> User Entries->Advanced and simply added the bin/resources folder. That worked perfectly, the program finds the properties file, all is well. However, I am dissatisfied that my previous efforts failed without me really understanding why. It seems that each one should have worked.
Additionally, I want to ensure that I fix this problem in such a way that it is captured by the code I check in so that subsequent users do not have to go through the same steps. My solution is thus not very satisfactory as I am not sure what actual piece of code changed, and thus cannot verify that the 'fix' is checked in.
How do you find the actual definition that eclipse is using for its classpath? I had thought it would be the build.xml classpath definition, but that did not seem to be the case at all.
In Eclipse, there is a build classpath and a runtime classpath. There is also the build output location, which by default is bin. You don't want to add resources directly to bin because Eclipse can delete its contents when doing a clean build. What you need to do is add a resources folder in your project to contain any non-Java files that you want included in your build output.
To include the contents of this resources folder in the build output (bin), right-click the project and select Properties. In the Project Properties, select the Java Build Path section, then the Source tab.
Use the Add Folder... button to select the resources folder from your project, then OK to save the changes. At that point, Eclipse will automatically copy everything from resources into bin when it builds.
This is for a maven project:
Right click on project
click on run configurations
click on the classpath tab (Oxygen Eclipse)
click on user entries
click on Advanced
first radio selection default should be 'Add Folders'
click OK
Follow these steps to get this issue fixed:
Right click on Project
Click on Run As and select Run Configurations
Click on the classpath tab (Oxygen Eclipse)
Click on user entries
Click on Add External JARs.. and choose the downloaded JAR file
Click Apply and run your project...
Right Click on the project-name in Package Explorer, select Properties, select Java Build Path on the left, select Source tab on the right, click on Add Folder, browse through the project's directories to select the resources folder or whatever you need to add to the eclipse classpath, hit OK, again hit OK. Done.
If you don't want the properties file to be copied to the bin folder, you can try the following:
Right click your project, select Build Path, select Configure Build Path..
Select Libraries tab
Select Add class folder..
Add your resource folder.

Eclipse Java Project - Open Declaration always opens class file instead of .java

I'm posting this query after doing lots of googling and trying out different options for last few days. Is there an easy way to attach source in Eclipse? is the closest answer I found to my question.
From above post, I tried all solutions but each time Eclipse is opening .class file instead of .java file.
Eclipse Details:
Kepler Service Release 2
Build id:20140224-0627
Please let me know if this is a bug with Eclipse or if I am missing something.
If you are using Maven, try this:
Double click on your project in eclipse -> Maven -> Enable Workspace Resolution
This is most likely because Eclipse actually use the class file for declaring the item you have requested the declaration for, and not the source file you think it should be using.
The typical reason for this is if you have a jar file containing previously compiled classes on your build path before (or instead) of the project containing the source you want. This is very rarely desirable as it can take quite a while to discover that your edits do not take effect.
Examine your build path carefully and ensure it is as you want it to be.
Maven is likely supplying a Classpath Container to your project, and generating entries for the Java Build Path based on the pom.xml contents (the JRE System Library is another example of this). Its entries will not have source attachment unless the container decides it will, which is why the Installed JREs preference page offers to let you set Source Attachments. If the pom.xml has something like that, see if you can use it. If not, see if you can configure the Maven container from its context menu. Otherwise, you may simply be stuck.
I had this same problem: when I pressed F3 or tried to open Declaration (after right click) for any element, it took me to the bytecode .class file.
My solution:
I went to the project in Project Explorer:
right click on project name->Properties
Then in Porject Properties window,
Java Build Path->"Order and Export" tab
There I selected the folder with the .java files (source code), which was below the folder having the .class files -in my case "JRE System Library [java-11-openjdk-amd64]", and pressed 'Top' button on the right frame. Then the folder with source files moved to the top.
Finally, pressed "Apply and Close" button.
That solved my problem forever and ever.
The folder
I installed a plug-in which allows me to download the sources for a maven project.
I'm not by my develop machine right now, but I think it was this:
http://marketplace.eclipse.org/content/m2e-dynamic-sources-lookup#.U8gjGWIaySM
With this, you can select a project an have maven download the sources and make then available when you navigate the code in Eclipse. All without "leaving" Eclipse.
Check into your project modules the Java Build Path, look inside the Source tab and verify if you have "Allow output folders for source folders" checkbox flagged; in this case you just need to uncheck it and you will be able to see again the right source from java files.
This is related to the Call Hierarchy in Eclipse IDE.
If you are in a situation, where you're seeing duplicate methods inside the call hierarchy showing Members calling 'myMethod(...)', double-clicking on one of the listed methods may open the .class file instead of the .java file.
When this happens, the solution is to set the search scope in the call hierarchy to Project instead of Workspace.
After setting the search scope, you won't be seeing duplicate methods listed in the call hierarchy, and double-clicking on the listed methods will only open the .java files.
(Using Eclipse DevStyle Dark Theme)

Putting resources into my eclipse project

I've just changed to eclipse an I'm used to manually managing my project structure with Windows filesystem (compiling my .java files into bin with a batch-script). Whenever I wanted my classes to use a .png-file, I simply put this file into the .bin folder.
If I put my .png-files in the bin-folder of the eclipse project as I'm used to, the classes will be able to load the pictures just fine. But sometimes, I don't know when, Eclipse simply deletes the bin-folder and creates a new one based on the contents of the src folder (which doesn't have my .png files). Putting the files into the src-folder (again, manually from the windows file-system) doesn't actually put them in my Eclipse project.
So what I want to ask is: Why/when does Eclipse completely rebuild the bin-folder. And how can I put resources, like .png-files, into my Eclipse project?
of course there is no point in putting files in the bin as it may be rebuilt by Eclipse whenever needed and you'll loose your changes.
by default Eclipse is set up to rebuild the project automatically, you can see that by looking in the "Project" menu entry, you can force Eclipse to rebuild your project by choosing "clean" in that "Project" menu.
So now you should have more control over when Eclipse is building your project.
Regarding adding resources files, I'm not sure what your current project directory structure is, in order to add / remove directories in the project, right click on the project root in the project Explorer side pane and choose "Properties" entry (at the bottom), then you can go to "Java Build Path" -> "Source" tab and manage the project directories.
last, if you change files on your disk and not through Eclipse, right click on the project root and choose "refresh" entry.
Hope this helps.

accidentally deleted the default output folder in eclipse

I was rearranging some files in eclipse, and I accidentally deleted the default output folder. This is the location where eclipse stores the class files. Can anyone show how to recover it? Or rebuild it from the .java files?
EDIT:
I just restored the project from backup files. So this question is no longer relevant. Unless some future reader did not backup their application before making this same mistake.
from the menu bar select:
Project->Clean...
That will bring up a dialog box to select which projects to rebuild. The bin directory should be automatically regenerated.
Recompile your project, and Eclipse should create it again (default name is bin on some versions)
If not, navigate to your project settings, and go to
Java Build Path > Sourceand at the bottom you will see the default output folder. You can create it again by using the Browse button, and then Create new folder
All you need to do is re-compile your project, For that you can select your Project and do Ctrl+B or just select Project->Build Project
EDIT:
As you mention in the comments that you still do not see the class files. Please check the Default output folder by selecting your project Right-click Build Path -> Configure build path... then select Source tab and see what the Default output folder: mentioned there, your class files must be created there.

Where do I put the txt file that I want to read in Java?

If I am building an application in Eclipse using Java, and I want this application to read from a txt file, where should I put the txt file in the package in order to not have to specify its path (so that I can just say "file.txt"). I thought that it was supposed to go in the src folder, but it's not working.
Right click the project folder and click New -> file. The file will be in the Project folder and not in the source folder.
Put the file in the folder from where you run your Java application (your current/working folder). If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder. To create the file in the Eclipse project, you can simply drag-and-drop it there or right-click on the Eclipse project and create a new file.
The way this can be done is using .getResourceAsStream("file.txt")
SO thread
Downvoted for a correct answer? Wierd...
If you don't want to specify a path, and want to open a File from the file system using the java.io.File API, then put it in the working directory.
In Eclipse, the working directory defaults to the root level of your project, but you can modify it (and also review what it is set to) in an Eclipse Run Configuration. They are accessible under the "Run > Run Configurations..." menu option, and the working directory setting is under the "Arguments" tab for Java programs.
The important thing is for the directory containing your file to be on the classpath. When you're using Eclipse's run dialog for the settings of your project, there's a tab for classpath. You can use it learn what's already on the classpath, and to make additions if you want to.
As mentioned above by #tschaible, the Eclipse working directory defaults to the root of the project. Therefore, the program will look for the file from the root level.
However, if you intend to run the program from the command line, it acts differently because the working directory defaults to the folder that you are running the java file from (aka the src folder). Therefore, it is important to keep this discrepancy in mind when testing your java program from the command line.

Categories

Resources