unable to find properties file in intellij idea classpath - intellij-idea

I have spent last 2 hours just to add a properties file in my IntelliJ IDEA classpath. I am an Eclipse user and find it embarrassing that I am unable to do it in IDEA. I have a .properties file in my IntelliJ IDEA project and it's added in the classpath but still at runtime I am getting exception that file is not present.
I have followed all the answers in this question Add a properties file to IntelliJ's classpath
I have done the following things:-
1)
Go to Project Structure.
Select your module.
Find the folder in the tree on the right and select it.
Click the Sources button above that tree (with the blue folder) to make that folder a sources folder.
2)I have checked that settings->compiler->resource patterns has entry for ?*.properties
3) I have added the below tag in my pom.xml
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
Can someone help me here?
Further update. It's a maven web application project. I am trying to access the .properties file in my servlet. My servlet sits in src/main/java/some_package and my .properties file sits in src/main/resources/some_package.
try {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("package\\myProperty.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println("reading the property file " );
System.out.println("prop1 =" + prop.getProperty("prop1"));
System.out.println("prop2 = " + prop.getProperty("prop2"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

I found that you should not mention .properties while looking for the file name
IF file is say abc.properties then just look for abc instead of abc.properties

Generally, marking the right directories as source ones does solve the problem, but I found there's another important detail. You need to make sure that your working directory (can be found in Run Configurations) is set correctly. In my project it was set to the wrong directory for some reason, and since Java tries to find the *.properties file in the working directory (unless you specify an absolute path in your code), the client.properties in my properties wasn't found - it just wasn't present in the "wrong" working directory.
P.S. I'm a junior dev with little experience and this is my first answer here, so I may not have described everything perfectly and/or correctly, but I spent 2+ hours trying to figure out why my properties file wasn't loading, so I hope this helps someone who's in the same predicament. Happy coding :)

Related

Intellij IDEA overwrites 'i' character with ASCII of 'ı' in gradle-wrapper.properties file

There is a weird problem which I am not sure about its source.
I am using Intellij IDEA (2016 3.3) and Gradle (v3.3). I use Windows 10, Turkish OS.
Gradle has a wrapper properties file. (./gradle/wrapper/gradle-wrapper.properties)
The content of that file, which is generated by Gradle:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
When I open a Gradle project in Intellij, the last line of that file turns into this:
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-b\u0131n.zip
Intellij overwrites bin into b\u0131n. I checked what \u0131 refers to, and it is ASCII code for 'ı' letter.
And because the URL is broken, I can't build the project.
There are a lot of 'i' letter in that file (not mentioning the whole project), but somehow Intellij turns that specific 'i' in 'bin' into ASCI of 'ı'.
I have this problem for long, but for the first time Intellij insists overwriting it when I try to correct the letter manually. The only difference this time is that I created a multiple-modules containing project which means there are more than one gradle-wrapper.properties file.
Do any of you know why and how to solve this?
After some research, I found the origin of the bug and it is not Intellij IDEA but Gradle.
#Input
public String getDistributionUrl() {
if (distributionUrl != null) {
return distributionUrl;
} else if (gradleVersion != null) {
return locator.getDistributionFor(gradleVersion, distributionType.name().toLowerCase()).toString();
} else {
return null;
}
}
https://github.com/gradle/gradle/blob/master/subprojects/build-init/src/main/groovy/org/gradle/api/tasks/wrapper/Wrapper.java#L314
toLowerCase() method in here uses my locale (tr-TR) so the output of "BIN".toLowerCase() is "bın".
I added an issue in Gradle-dev Google group and suggested a solution.
In build.gradle, adding;
task wrapper(type: Wrapper) {
gradleVersion = '3.3'
distributionUrl = "https://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
}
solved it for me.
Check here for further info.

eclipse cdt plugin: How to programmability apply c code formatter

I'm writing a plugin for eclipse(Kepler) CDT on windows 8.1.
My plugin extends eclipse and enables the user to create a project with a specific configurations.
I want all my plugin projects to be with the same c code format. So in my plugin code, when creating the project files and configuration, I added the following code in order to add the wanted format:
ProjectScope scope = new ProjectScope(project);
IEclipsePreferences ref = scope.getNode("org.eclipse.cdt.core");
ref.put("org.eclipse.cdt.core.formatter.lineSplit", "100");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_parameters_in_method_declaration", "18");
ref.put("org.eclipse.cdt.core.formatter.brace_position_for_block", "next_line");
ref.put("org.eclipse.cdt.core.formatter.brace_position_for_method_declaration", "next_line");
ref.put("org.eclipse.cdt.core.formatter.tabulation.char", "space");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_arguments_in_method_invocation", "18");
ref.put("org.eclipse.cdt.core.formatter.alignment_for_constructor_initializer_list", "16");
ref.flush();
This code really does its job and configures the format as I want, but the generated format exists only in the project properties, and not being applied yet. If I want to apply the format and cause the files to show with this format, I have to click on apply button, or to press CTRL+SHIFT+F.
Do you know about any way to apply the format programmability, though each project will be generated with its auto-generated files that are formatted already?
You should be able to find the command ID using either the Eclipse menu spy (Alt-Shift-F2) or by looking up the key binding for the formatter in the key preferences or by importing the cdt.ui plugin as source plugin into your workbench.
When you have the command ID, then you can execute it programmatically using the command service.
I found the solution to my problem. thanks #Bananewizen for helping!
I found that the command id behind the ctrl+shift+f is: ICEditorActionDefinitionIds.FORMAT
and I succeeded to run the command programmability, but I had to handle the file loading in order to format its code. so I implemented IPartListener, and in partOpened function, I wrote:
#Override
public void partOpened(IWorkbenchPart part) {
try {
IFile file = (IFile) part.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
final String cmdName = ICEditorActionDefinitionIds.FORMAT;
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
try {
handlerService.executeCommand(cmdName, null);
} catch (Exception e) {}
} catch (Exception e) {}
}
Now the handle is found and it works well!

zip dependencies to a file does not work any more

basing on Gradle : Copy all test dependencies to a zip file
I created
task zipDeps(type: Zip) {
from configurations.testCompile.allArtifacts.files
from configurations.testCompile
exclude { details -> details.file.name.contains('servlet-api') }
exclude { details -> details.file.name.contains('el-api') }
exclude { details -> details.file.name.contains('jsp-api') }
exclude { it.file in configurations.providedCompile.files }
archiveName "${rootProjectName}-runtime-dependencies_full.zip"
doLast{
ant.copy (toDir : "$buildDir/libs/") {
fileset(file:"$buildDir/distributions/${rootProjectName}-runtime-dependencies_full.zip")
}
}
}
This worked fine until I migrated to gradle 2.0. If i leave that code like it was, the task is executed in the beginning and nothing happens at all. If I add << to the task and make it dependent to my war build task, at the end of the war build it claims to be up-to-date but nothing has happened.
One of my problems seems to be that the fileset to be copied is not created at all.
What can I do to get that stuff working again?
The task won't be executed in the beginning, but calling .files resolves the configurations too early. The first from line needs to go (it's redundant and also calls .files when it shouldn't). The doLast block is suspicious and should probably be turned into a separate Copy task. Instead of the second from and last exclude, try from (configurations.compile - configurations.providedCompile).

JFrame in remote between JDK 5 (Server) and 6 (Client - VisualVM)

So I have a little trouble on the opening of a JFrame. I searched extensively on the net, but I really can not find a solution ...
I explained the situation:
I need to develop an application that needs to retrieve information tracking application while meeting new safety standards. For that I use JMX that allows monitoring and VisualVM to see these information.
I therefore I connect without problems (recently ^ ^) to JMX since VisualVM.
There is thus in a VisualVM plugin for recovering information on MBean, including those on Methods (Operations tab in the plugin).
This allows among others to stop a service or create an event.
My problem then comes when I try to display a result of statistics.
In fact, I must show, at the click of a button from the list of methods in the "Operations", a window with a table in HTML (titles, colors and everything else).
For that I use a JFrame:
public JFrame displayHTMLJFrame(String HTML, String title){
JFrame fen = new JFrame();
fen.setSize(1000, 800);
fen.setTitle(title);
JEditorPane pan = new JEditorPane();
pan.setEditorKit(new HTMLEditorKit());
pan.setEditable(false);
pan.setText(HTML);
fen.add(pan);
return fen;
}
I call it in my method:
public JFrame displayHtmlSqlStatOK_VM(){
return displayHTMLJFrame(displaySQLStat(sqlStatOK, firstMessageDate), "SqlStatOK");
}
The method must therefore giving me back my JFrame, but she generates an error:
Problem invoking displayHtmlSqlStatOK_VM : java.rmi.UnmarshalException: error unmarshalling return; nested
exception is:
java.io.InvalidClassException: javax.swing.JFrame; local class incompatible: stream classdesc serialVersionUID =
-5208364155946320552, local class serialVersionUID = -2386951414768123374
I saw on the internet that this was a version problem (Serialization), and I believe strongly that it comes from the fact that I have this:
Server - JDK5 <----> Client (VisualVM) - JDK6
Knowing that I can not to change the server version (costs too important ...) as advocated by some sites and forums.
My question is as follows:
Can I display this damn window keeping my current architecture (JDK5 server side and client side JDK6)?
I could maybe force the issue? Tell him that there's nothing bad that can run my code? Finally I'm asking him but he does not answer me maybe to you he will tell you ... (Yes I crack ^^).
Thank you very much to those who read me and help me!
If you need more info do not hesitate.
EDIT
The solution to my problem might be elsewhere, because in fact I just want a table with minimal formatting (this is just for viewing application for an for an officer to have his little table him possibly putting critical data in red...).
But I have nowhere found a list of types that I can return with VisualVM ... This does not however seem to me too much to ask.
After I had thought of a backup solution, which would be to create a temporary HTML file and open it automatically in the browser, but right after that is perhaps not very clean ... But if it can work ^^
I am open to any area of ​​research!
It looks like you are sending instance javax.swing.JFrame over the JMX connection - this is a bad idea.
Well good I found myself, as a great :)
Thank you bye!
..........
Just kidding of course I will give the solution that I found ^ ^
So here's what I did:
My display to be done on the client (normal...) my code to display a JFrame that I had set up on the server was displayed obviously ... On the server xD
I didn't want to change the customer (VisualVM) to allow users maximum flexibility. However I realized that to display my HTML table to be rendered usable (with colors and everything) I had to change the client (as JMX does not support the type JFrame as type back an operation).
My operation running from the MBeans plugin for VisualVM, it was necessary that I find the source code for it to say "Be careful if you see that I give you the HTML you display it in a JFrame".
Here is my approach:
- Get the sources
The link SVN to get sources VisualVM is as follows:
https: //svn.java.net/svn/visualvm~svn/branches/release134
If like me you have trouble with the SVN client includes in NetBeans because you are behind a proxy, you can do it by command line:
svn --config-option servers:global:http-proxy-host=MY_PROXY_HOST --config-option servers:global:http-proxy-port=MY_PROXY_PORT checkout https: //svn.java.net/svn/visualvm~svn/branches/release134 sources-visualvm
Putting you on your destination folder of course (cd C:\Users\me\Documents\SourcesVisualVM example).
- Adding the platform VisualVM
NetBeans needs the platform VisualVM to create modules (plugins) for it. For this, go to "Tools" -> "NetBeans Platforms".
Then click "Add Platform ..." at the bottom left of the window and select the folder to the bin downloaded at this address: http:// visualvm.java.net/download.html
You should have this:
http://img15.hostingpics.net/pics/543268screen1.png
- Adding sources in the workspace (NetBeansProjects)
Copy/paste downloaded sources (SVN from the link above) to your NetBeans workspace (by default in C:\Users\XXX\Documents\NetBeansProjects).
- Ouverture du projet du plugin MBeans
In NetBeans, right click in the Project Explorer (or go to the menu "Files") and click "Open Project ...".
You will then have a list of projects in your workspace.
Open the project "mbeans" found in "release134" -> "Plugins", as below:
http://img15.hostingpics.net/pics/310487screen2.png
- Changing the file "platform.properties"
To build plugin you must define some variables for your platform.
To do this, open the file platform.properties in the directory release134\plugins\nbproject of your workspace.
Replace the content (by changing the paths compared to yours):
cluster.path=\
C:\\Program Files\\java6\\visualvm_134\\platform:\
C:\\Program Files\\java6\\visualvm_134\\profiler
# Deprecated since 5.0u1; for compatibility with 5.0:
disabled.clusters=
nbjdk.active=default
nbplatform.active=VisualVM_1.3.4
suite.dir=${basedir}
harness.dir= C:\\Program Files\\NetBeans 7.1.2\\harness
- Changing the class XMBeanOperations
To add our feature (displaying an HTML table), you must change the class that processes operations, namely the class XMBeanOperations in package com.sun.tools.visualvm . modules.mbeans.
At line 173, replace:
if (entryIf.getReturnType() != null &&
!entryIf.getReturnType().equals(Void.TYPE.getName()) &&
!entryIf.getReturnType().equals(Void.class.getName()))
fireChangedNotification(OPERATION_INVOCATION_EVENT, button, result);
By :
if (entryIf.getReturnType() != null &&
!entryIf.getReturnType().equals(Void.TYPE.getName()) &&
!entryIf.getReturnType().equals(Void.class.getName())) {
if (entryIf.getReturnType() instanceof String) {
String res = result + "";
if (res.indexOf("<html>") != -1) {
JFrame frame = displayHTMLJFrame(res, button.getText());
frame.setVisible(true);
}
else
fireChangedNotification(OPERATION_INVOCATION_EVENT, button, result);
} else
fireChangedNotification(OPERATION_INVOCATION_EVENT, button, result);
}
With the method of creating the JFrame that you place above "void performInvokeRequest (final JButton button)" for example:
// Display a frame with HTML code
public JFrame displayHTMLJFrame(String HTML, String title){
JFrame fen = new JFrame();
fen.setSize(1000, 800);
fen.setTitle(title);
JEditorPane pan = new JEditorPane();
pan.setEditorKit(new HTMLEditorKit());
pan.setEditable(false);
pan.setText(HTML);
fen.add(pan);
return fen;
}
We can see that we already did a test on the return type, if it is a String which is returned, if the case, if we see in this string the balise , then we replace the result of the click by opening a JFrame with the string you put in, what makes us display our HTML code!
- Creating a .nbm
The file .nbm is the deployment file of your plugin. Simply right-click your project (in the Project Explorer) and click on "Create NBM".
Your file .nbm will be created in the folder "build" the root of your project.
- Installing the plugin in VisualVM
To install your plugin, you must just go in VisualVM, go into "Tools" -> "Plugins" tab and then "Downloaded", click "Add Plugins ...". Select your plugin .nbm then click "Install". Then follow the instructions.
Useful Sources
http: //docs.oracle.com/javase/6/docs/technotes/guides/visualvm/
http: //visualvm.java.net/"]http://visualvm.java.net/
http: //visualvm.java.net/api-quickstart.html (Créer un plugin VisualVM avec NetBeans)
Thank you very much for your help Tomas Hurka ;)

How can I set struts.custom.i18n.resources properties to a specific folder?

Java 1.6, Struts 2.0.11, Windows OS
What configurations do I need to make in struts.xml to place module-specific labels based properties file outside src package folder?
Reason: Group all internationalization-based properties in seperate module-based folder?
WEB-INF
|_classes
| |__com
| |__xyz
| |__Hellofoo.class
|
|__ struts.xml
|
|__props
|__xyz
|_ en.properties [ English Labels ]
jp.properties [ Japanese Labels]
spn.properties[ Spainish Labels ]
I believe that you have to specify this either in struts.xml file or in property file like
struts.custom.i18n.resources=global-messages, image-messages
or in xml file like
<constant name="struts.custom.i18n.resources" value="global-messages, image-messages" />
Alternatively you can use some sort of Listener to customize it as per your need.
IMO text resources should be organized according to region and package, but that's a matter of choice.
Now this is not really an answer to your specific question. However i think it can be useful for people(like me) who stumble across this page with the question "How can I set struts.custom.i18n.resources properties to a specific folder (inside the src folder, but not right below it)
Now when i set the following in struts.properties, it does not work
struts.custom.i18n.resources=resources/locale-bundles/label-values,resources/locale-bundles/error-values
However if i set it in struts.xml, it does. (No idea why)
<constant name="struts.custom.i18n.resources"
value="resources/locale-bundles/label-values, resources/locale-bundles/error-values" />
I found a solution
first in struts.properties struts.custom.i18n.resources=globalMessages
then, add the below code to the your StartupServlet or some other place that will execute where the server start
URL[] urls;
try {
File file = new File("/your path");
URL url = file.toURI().toURL();
urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
LocalizedTextUtil.setDelegatedClassLoader(cl);
LocalizedTextUtil.addDefaultResourceBundle("globalMessages");
} catch (MalformedURLException e) {
e.printStackTrace();
}