Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, 22 November 2010

Strange errors while using maven (dependency mess)

(Java&Maven)


I have two projects built with maven. One of them includes the other.
The new requirement that I have done is working in the original (common) project, but, even the simplest call to this functionality from the upper project results with an error (in current case with ClassNotFoundException, but this can be any time of error).

Here is one of the thing that should be done when you don't have the clue - check dependencies. By using maven it is possible to have a mess with them if you do not use exclusions within dependency tags. This way you can get two versions of the same JAR downloaded, and the older version used - so you got the class not found exception.

Useful tool is within Eclipse Galileo: open pom.xml and check all items within Dependency Hierarchy. From there you can build a list with duplicated and conflicted versions of JARs, and a list of exclusions you need to set in same pom.xml file.
After altering pom.xml file, check the hierarchy again - you should have one position in the hierarchy for each resolved dependency (or at least positions within same library in the hierarchy without any conflicts).

Wednesday, 1 July 2009

Uncompilable source code

Got this at the very end of the java file.

I have solved this one only by removing and reentering new line sign after class definition in that file.
Many other strange errors can be solved this way.

Friday, 6 February 2009

Eclipse Tomcat in more than 45 seconds

(Eclipse 3.3 - for Eclipse 3.4+ check comments on this post)

Do you have some web application that cannot be started in Eclipse's Tomcat in 45s, and you get that annoying time out message?

  • Go to Windows > Preferences in Eclipse
  • Search for Server
  • Set Server timeout delay at you're own will
Here is the list of values in drop down:
Preference    TimeOut
Shorter       11s
Short         22s
Normal        45s
Long          90s
Longer        180s
Unlimited     unlimited
Source: https://bugs.eclipse.org/bugs/show_bug.cgi?id=151993

Friday, 16 January 2009

Debugging log4j itself

Problem: You cannot find out which log4j.properties file is used.

Try running VM with parameter
-Dlog4j.debug

You'll get info about log4j properties in console.

See: http://logging.apache.org/log4j/1.2/manual.html

Saturday, 25 October 2008

GWT under NetBeans memory problem

To solve JavaHeapSpace when starting some GWT project under NetBeans, edit file
/nbproject/build-gwt.xml

under next path in this XML
project/target[name="-post-compile"]/java

add attribute/value pair inside java tag
maxmemory="128M"

Monday, 14 January 2008

Current directory in Java

Here is something that I need all the time:

Current directory can be retrieved with:
System.getProperty("user.dir");

Some other properties from System are also very useful (here is the list of all properties)

Saturday, 22 December 2007

String's methods with regular expressions

The next example is for method replaceFirst(String regex, String replacement) which should find first substring that matches given regular expression and change it with replacement.

What if you want to replace only the first substring that is equal to "[sometnig]" - this represents regular expression that will match every single character from the word 'something', and, so 
a.replaceFirst("[something]", "anything");
will return (bold substrings are match and replacement):
  • if a="this is a [something] test"
    then "anythinghis is a [something] test"
  • if a="[something]"
    then "[anythingomething]"

What we need here is static method Pattern.quote(String s) which will make regular expression that matches string s as is. Thus, call
a.replaceFirst(Pattern.quote("[something]"), "anything");
if a="this is a [something] test"
will return "this is a anything test".

BTW: This is just for replacing only the first substring matched. If you want to replace all substring that are matched, you should use String's method replace. Also, there is method replaceAll which, again, uses String parameter for regular expression pattern matching.

Popular Posts