Using eclim whenever I type log. the following packages are automatically imported:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
The following line is also added the start of my class:
private static final Log log =
LogFactory.getLog(ClassName.class);
Not sure if this behavior is coming from Eclipse or Eclim. Is it possible to disable this?
This is part of vim configuration, I suggest you to read this to solve your issue : http://eclim.org/vim/java/logging.html
In short, you may add
let g:EclimLoggingDisabled = 1
through :VimSettings or to your .vimrc.
Related
I am trying to migrate an existing project from direct log4j to slf4j with log4j binding.
Also I am upgrading the the version of log4j from 1.2 to 2.6
Some of the common code change are :-
1.
import org.apache.log4j.Logger;
.
.
.
private final static Logger log = Logger.getLogger(SearchXYZ.class);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.
.
.
private final static Logger log = LoggerFactory.getLogger(SearchXYZ.class);
2.
import org.apache.log4j.Logger;
.
.
.
private static final Logger logger = Logger.getLogger(XYZ.class);
.
.
.
logger.fatal("FAILURE", throwableObject);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
.
.
.
private static final Logger logger = LoggerFactory.getLogger(XYZ.class);
private static Marker fatal = MarkerFactory.getMarker("FATAL");
.
.
.
logger.error(fatal, "FAILURE", throwableObject);
Removed Appenders.
and so on.
One place I'm stuck is Configurator file.
AppConfigLog4jConfigurator.configureForBootstrap();
It gives compile time error saying :-
class file for org.apache.log4j.spi.Configurator not found
What does this function do? What is a possible replacement for this?
First, I am not really sure why you are switching to the SLF4J API since the Log4j 2 API supports everything SLF4J does and much more. In my own code I have found that switching only requires changing the imports and LoggerFactory to LogManager.
Configurator is a class in Log4j 1 that is used to configure Log4j. It is similar to the Configurator class in Log4j 2. You probably want to call one of the initialize methods.
I have defined the following live template in IntelliJ:
private static final Logger log = LoggerFactory.getLogger($CLASS_NAME$.class);
I use it to insert logger variable to a class.
Is it possible to define so that template also adds
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
to the file if these definitions are still absent?
Define it fully in the Live template:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);
and IntelliJ should auto reformat the expression to an import. (Assuming you already have the lib JAR downloaded and configured with IntelliJ).
Edit: As comment says: the Shorten FQ Names check-box should be checked (which it is by default)
Tested with IntelliJ IDEA 15.0.4
Now its possible to add live templates with static imports:
You have to check static import in Options
#org.junit.Test
public void should$EXPR$when$CONDITION$() {
org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue()));
}
i can't import android.view.TextureView.
All other import work except android.view.TextureView. i download the textureView.java but can't compile, because of other unknown type like private HardwareLayer mLayer.
i see that every body import it without any error.
TextureView was added in API level 14. In order to use it, you must target your app to at least this API level.
My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:
static import only from classes and interfaces
Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?
Update:
per #Ted's request, the constant declaration in the referenced file:
public static final String NULL = "<NULL>";
and the (bowdlerized) reference in the referring file:
import static my.path.MyClass.NULL;
My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:
import static pkg.Class.staticField;
do this:
import static pkg.Class.*;
Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...
I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue
I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.
I had the following:
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
I removed the import static org.junit.Assert.fail; (no idea how I managed to get that in there in the first place) and all is now working.
I accidentally set test directory as source. And Test sources were considered as source files.
sourceSets.main.java.srcDirs 'src'
| -- src
| -- main
| -- test
Fix:
sourceSets.main.java.srcDirs 'src/main'
Some how same solution mentioned by #m-watson
I have replaced
import static org.junit.Assert.assertThrows;
With
import static org.junit.jupiter.api.Assertions.assertThrows;
and it worked
I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file.
I don't want to hard code the file path in the source, and so I want to get that path from the application.conf.
The problem is that I don't know how to access these properties from the java class.
I tried this:
Configuration.root().getString("file.path")
But it ends with a NullPointerException.
Am I wrong in assuming that there's a global Configuration instance that I can use?
Thanks.
Try Play.application().configuration().getString("your.key")
As noted in the comment (nico_ekito), please use play.Play and not play.api.Play. play.api.Play is for scala controllers (see comment by Marcus biesior Biesioroff)
Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.
Even if it seems simple, here is the scala way to get properties from configuration file :
Play 2.0 and 2.1 :
import play.api.Play.current
...
Play.application.configuration.getString("your.key")
Play 2.2 and +
import play.api.Play.current
...
current.configuration.getString("your.key")
Using Typesafe config
import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");
From Play 2.4 and + it is better to use dependency injection to access Configurations:
import play.Configuration;
import javax.inject.Inject;
#Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");
Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
ConfigFactory.load().getString("my.var");
In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")
Use as following (Tested in Play 1.2.5)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file
As a reference to access it from the template (for play < 2)
play.configuration['your.key']
As folks have mentioned, Play.application.configuration no longer exists.
In Play Scala 2.3.x, to read a value from conf/application.conf, you can do the following:
import play.api.Play.current
...
current.configuration.getString("key")
In Play 1.2.x
import play.Play;
...
String version = Play.configuration.getProperty("application.version.number", "1.1.1");
where the second parameter is the default value
Import this
import com.typesafe.config.Config;
and write the below lines
private Config config;
this.config = ConfigProvider.config();
String value = this.config.getString("fieldFromConfigFile");
import play.Play;
String myVal = Play.configuration.getProperty("your.key").toString();
i use this in my app and it works
Dont forget to import play.Play. Hope it'll gives you help
Starting from version 2.5 please use play.Application class which should be injected and then
application.config().getString("your.property.here")
For Java Playframework:
In Application.conf, you can put something like that:
email="example#gmail.com.pe"
some class:
import play.Play;
String email = Play.application().configuration().getString("key") // key ->email