How to force a refresh on the Project Explorer view? - java

I created a Wizard that when finished, adds two files at the Project Explorer.
One of them should be hidden, but when I press the Finish button at my wizard, Eclipse doesn't refresh the view automatically and it keeps showing the file. It just hide it when I press F5.
There's a way to force it to refresh the Project Explorer, right after I finish the wizard?

Another option is in eclipse
Go to Windows-->Preferences-->General-->Workspace--> and check the box for Refresh Automatically ("refresh using native hooks or polling")
If this option is turned on then the workspace resources will be synchronized with their corresponding resources in the file system automatically.
Note: This can potentially be a lengthy operation depending on the number of resources you have in your workspace.
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.user/reference/ref-9.htm

It sounds like you aren't using Eclipse resources API to make file system changes. You have two options:
The best option is to use Eclipse resource API instead of java.io when working with contents of Eclipse projects. See org.eclipse.resources plugin. Start with ResourcesPlugin.getWorkspace().getRoot().getProject( "name" ). Use IProject, IFile and IFolder API.
Alternatively, especially if your wizard is invoking code that isn't eclipse-aware, you need to invoke refresh after you are sure that all java.io based file system operations have been completed. Use refreshLocal() method, which is available on IProject, IFile and IFolder classes. For instance, the following snippet refreshes all contents of a given project. This is typically an overkill, so you will want to narrow down the scope as much as possible before invoking refresh.
ResourcesPlugin.getWorkspace().getRoot().getProject( "proj" ).refreshLocal( IResource.DEPTH_INFINITE, new NullProgressMonitor() );

Another option is to use "External tools" to call your tool, and check options to refresh and maybe compile target project/s in "Refresh" and "Build" tabs.
Run -> External Tools -> Externa tools configuration

Related

Intelli-J: cannot create class-file?

This is honestly a tiny problem but it's keeping me from proceeding with my small Java practice app.
I'm currently practicing making an API call in Java. I was trying to create a simple class file called "Film". However, when I try to create it as a class-file, Intelli-J keeps telling me that it is unable to create a class-file. I'm trying to set this file up in a folder called "models". All of these class files are going to be utilized as part of an app that performs API calls.
Is there some naming convention I haven't followed? I've been Googling but haven't really found anything.
More directly, based on what #Vishal Jumani touched on, you need to tell IntelliJ what directories are 'source' or 'test' directories.
A directory structure you can use as an example, but you can use whatever you wish.
Right click on the directory you wish to mark as 'root' --> find 'Mark Directory As' --> Select 'Sources Root'
Now you should be able to add java classes in the directory without IntelliJ interrupting you!
This would usually be because of the way your project is setup. It may not be setup correctly to indicate where your source code is.
To setup your project, in the Project Tab, click on the top most folder and select F4. This should bring up the Project Settings dialog window.
Now click on Modules in the LHS, and then select the Sources Tab on the RHS
Select your src folder and click on the Sources button, to indicate this is your source folder. Now IntelliJ is aware that this is where your code is.
Click on Apply and OK. This should close the Dialog Window.
Now right click on the src folder and then select New -> Java Class to create your Java class

How do set all Eclipse projects to same JVM at once?

I am working on a large project with about 60 different Eclipse projects. I am looking for a method of setting the JVM for all projects at once. I know that I can right click on each one, go to Build Path, and manually change it to a certain execution environment (or JVM or workspace default), but it appears that this must be done one at a time.
Thanks.
I don't know of any way to do it via the UI, but you could use a File search+replace to modify the .classpath files directly.
From the menu, toolbar, or keyboard shortcut open the File Search dialog. The text you want to search for, assuming your projects all use Execution Environment to specify their JRE libraries, is something like StandardVMType/JavaSE-1.7 (that's the text for the JavaSE-1.7 Execution Environment). Specify .classpath as the file name pattern, and the Scope you want, then click Replace...
In the Replace Text Matches dialog, specify the new Execution Environment text, for example StandardVMType/JavaSE-1.8. OK or Preview> to continue with the replacement.
Eclipse will re-build all the projects and once it completes they should all be referencing the new Execution Environment.

How do I add global libraries to IntelliJ IDEA?

I've tried adding .jar files to the Global Libraries section in settings, but whenever I create a new project, those libraries still have to be manually added to the project.
I've consulted the documentation on Jetbrains's site (https://www.jetbrains.com/idea/help/configuring-project-and-global-libraries.html) and I seem to be doing everything correctly.
With only the Welcome to IntelliJ IDEA window open, I've performed the following steps:
Open Default Project Structure options with CMD+;
Select Global Libraries from the left pane.
Click the + in the second pane and select Java.
Navigate to and select the jsoup-1.8.2.jar file which is the library I am attempting to make global and then select OK.
Click OK to close the Default Project Structure options window.
Select Create new Project
Select Java from the left panel and then select Next.
Select Next again.
Name the project and select Finish.
At this point the only External Libraries added is <1.6>.
So I have to proceed by manually adding the library to the project.
How do I set IntelliJ IDEA to load specific libraries automatically when creating new Projects?
Global libs are "global", so it can be used in multiple projects, so the real question is How does IJ know if you want to use this global lib in to "this" project ?
b/c the way "global libs" woks is you could have python virtual-env as global lib, you could have django libs there.. any named-custom set of jars could be a global lib etc..
so how does ij know-- you need python or django etc in this project? that's why you have to tell/add it manually deciding which one is needed for this project.

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)

Reload Class When Dependent Resource Changes in Spring

You know how Eclipse reloads classes automatically running in Tomcat when resource files they depend on such as spring context files are updated so that you don't have to re-start Tomcat? How do I make a class dependent on a resource file so that Eclipse re-publishes it when the resource file changes? Eclipse would re-publish Spring classes when their dependent resource files are updated, but would not re-publish mine.
I'm not sure if it's Spring that's doing this for you. Are you developing in an IDE like Eclipse or Netbeans? What might be happening is as you make code changes your IDE is 'publishing' (i.e. re-deploying) your app code, so the app re-initializes every time the IDE publishes it, giving the illusion that Spring is reloading context files as they change.
This is useful with a small app but get's very annoying the larger your app gets.
If you're looking for this behavior for development, take a look at JRebel:
http://www.zeroturnaround.com/jrebel/
You say that you want the class to be republished ? Do you mean that you want the contents of the bean in the application context to be updated ? If that's the case, then what you can do is the following :
1) Write a file system monitor to monitor the resource(s) for changes. There's an example on google code
2) Have that file system monitor fire a custom Spring ApplicationEvent whenever the file / resource changes, providing that event the information for the resource, if necessary. ie the file name, the previous modified time, the last modified time, etc.
3) Have the bean you want to be updated implement ApplicationEventListener and reload the resource when it catches your file system monitor event.
A simple but working workaround can be to add an ant builder to your project. Steps to take:
read the article
here
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.user/gettingStarted/qs-93_project_builder.htm
create a simple ant file, that
contain a target, which touches
(simply changes the date of
modification) the dependent classes,
but nothing else (if you need help in it, let me know). Eclipse will provide you variables inside the script. You can print them easily with task. You will see the list in console.
right click on the project, and
press properties
open "builders" tab in properties
add your ant script as a builder to
the project
restrict the set of resources this
ant builder is called for. This can
be done in "build options" tab in ant
builder options. This way your
project will be fast, and the ant
script will only run for the changes
of the property file
set the set of resources (classes depending on properties) to refresh after running the ant script in "Refresh" tab
Set your ant script target to be called for "Auto build" in "target" tab. Others like after and before clean and manual build should be empty
You may redirect log of the ant script to file if you want. Otherwise it will open console view.
move your builder to be the first in the list of builders, since it must run before java builder
This is a workaround, and should work. It will have no bad side effect, since the content of java file will not change, and it will not affect the version control system, as well as the whole thing is workspace independent, if the ant script is in your project.

Categories

Resources