JsonTemplateLayout in Log4j2 - json

How to add a external file location in eventTemplateUri of JsonTemplateLayout instead of classpath ?
<JsonTemplateLayout eventTemplateUri= ??/>
What does classpath defaults to in this case ?

Related

java.lang.No classDefFound error while using PEXML for android in Libgx

I am using PEXML library for giving physics to bodies in my game. It worked very well in desktop but when I ran in android it is giving me error at code
playerPhysics = new PEXML(Gdx.files.internal("data/physics.xml").file());
and showing error
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/bind/JAXBContext; at com.hi5dev.box2d_pexml.PEXML.loadXML(PEXML.java:124) at com.hi5dev.box2d_pexml.PEXML.(PEXML.java:24)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.bind.JAXBContext" on path: DexPathList[[dex file "/data/data/com.myplayer.football/files/instant-run/dex/slice-slice_9...
Google search shows javax.xml.bind.JAXBContext is in the file javax.xml.bind.jar
Java needs to find this file at runtime. It looks for it in your Java classpath ( set of directories ).
You need this Jar to be in your runtime classpath
The error you wrote about shows you the classpath.
You can either
put javax.xml.bind.jar in the current classpath
or
Modify the classpath to include the location of javax.xml.bind.jar
Lookup how to modify the Java classpath for your operating system.

How to check if log4j2 has been configured or not

We're all familiar with this message when you don't provide a configuration for log4j2:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
How can I check if log4j2 is not yet configured so that I can initialize with a default configuration if needed?
I ended up creating my own ConfigurationFactory and register it with
-Dlog4j.configurationFactory
That way your ConfigurationFactory will know whether it's been invoked yet or not.
On the first call from your app to log4j2 code, log4j will configure itself before the method returns. If no config file is found, log4j will auto-configure with a default configuration which logs only errors to the console. So from your application's point of view there is never a time that log4j is not configured.
One idea is to check if a log4j2.xml file is in the classpath (using Class.getResource), and if it isn't call System.setProperty("log4j.configurationFile", pathToYourConfig). Note that this must be done before the first call to the log4j2 API.
An alternative:
Once you have a LoggerContext you can call
context.setConfigLocation(configLocation) where configLocation is a URI.
That will force a reconfiguration.

warbling a simple ruby sample with YAML dependency

I try to make a .JAR from a simple ruby sample. In MRI and jRuby i can run this without problem but when i try to warble the project i get error when i run the JAR file.
Errno::ENOENT: No such file or directory - configuration.yml
The configuration.yml file is not in the project folder because is needs to be editable so shouldn't be in the JAR included, optimally the path would be relative to the jar so that the YML file is in the same folder as the JAR file. So how could i do this ?
My bin\sample.rb
require 'yaml'
#conf = YAML::load_file "c:/test/configuration.yml"
##conf = YAML::load_file "c:\\test\\configuration.yml"
puts #conf['username']
My c:\test\configuration.yml
username: test
My .gemspec file
Gem::Specification.new do |spec|
spec.require_paths << '.'
}
My config\warble.rb
Warbler::Config.new do |config|
config.jar_name = "sample"
end
I warble with "warble jar -trace" and get no errors
I use jruby-1.7.4 and warbler-1.3.8.
Figured it out without using java as some solutions on the web suggest (but not for warble).
require 'yaml'
scriptpath = __FILE__[/(.*)\/.+\.jar!/]
yamlpath = "#{$1[6..-1]}/configuration.yml"
#conf = YAML::load_file(yamlpath)
puts #conf['username']
# => test

ClassNotFoundException: class JSONObject

I want a servlet to return json on ajax request. For that I imported org.json.jar. But I am getting ClassNotFoundException for the class 'JSONObject' when I hit the url of the servlet.
I have included the jar in classpath too.
Did you copy the jar file to your servlet container's lib directory? (like WEB-INF/lib)

Hadoop Configuration.addDefaultResource() not working

My following code does not produce expected output:
public static void main(String[] args) throws MalformedURLException {
Configuration.addDefaultResource("/home/some_user/conf.xml");
Configuration conf = new Configuration();
System.out.println(conf);
System.out.println(conf.get("color"));
assertThat(conf.get("color"), is("yellow"));
}
The property color is set in conf.xml file as follows:
<property>
<name>color</name>
<value>yellow</value>
<description>Color</description>
</property>
Looks like file conf.xml isn't getting incorporated in default configuration.
The documentation for Configuration.addDefaultResource(String param) says the param should be in classpath. I don't understand how to add file to the classpath when I am already giving the program full absolute path.
First observation: I don't know which version of hadoop you use but the addDefaultResource() has been deprecated for a very long time.
In the later versions of Hadoop the standard way to accomplish what you want is:
Configuration conf = new Configuration()
conf.addResource("path/to/file");
...
Regarding the classpath issue, you have to simply place the config file in the classpath. So you have to discover what the classpath is to(it is either an environment var or the one which you set with -classpath option). If you didn't use the -classpath option and there is no classpath environment variable then it is automatically set to the current directory (".")