Unable to load application.properties on Hadoop jar (NullPointerException) - java

I've looked at all kinds of answers for this problem. None of them work.
I have the following code:
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ApplicationConfig {
private static Logger LOG;
private String appConfigFileLocation = "application.properties";
private Properties appConfig;
private static ApplicationConfig instance;
public static ApplicationConfig getInstance() {
if(instance == null) {
instance = new ApplicationConfig();
}
return instance;
}
private ApplicationConfig() {
LOG = LoggerFactory.getLogger(this.getClass().getSimpleName());
appConfig = new Properties();
try {
LOG.info("Reading config from " + appConfigFileLocation);
appConfig.load(ClassLoader.getSystemResourceAsStream(appConfigFileLocation));
LOG.info("Done reading config from " + appConfigFileLocation);
} catch (FileNotFoundException e) {
LOG.error("Encountered FileNotFoundException while reading configuration: " + e.getMessage());
throw new RuntimeException(e);
} catch (IOException e) {
LOG.error("Encountered IOException while reading configuration: " + e.getMessage());
throw new RuntimeException(e);
}
}
}
I created a JAR file. The JAR file has application.properties at the root. I also copied the application.properties file in /etc/hadoop/conf and in the target/classes/ directory.
I use the hadoop jar command to execute the code.
But I keep getting the error: java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434)
Please help me at resolving this frustrating error!

Found the error.
hadoop jar checks in the Hadoop classpath. Even though the file was there in the Hadoop classpath, it didn't have read permissions from the Hadoop user.
A simple sudo chmod a+r /etc/hadoop/conf/application.properties did the trick!

Related

Camel-K does not recognize local package

I have a RouteBuilder class that is using its own Processor. When running locally in Camel using Maven, it runs fine. However, when I try to use camel-k, it says it cannot find the package. Is there something I need to do?
MyProcessor
package com.test.processor;
import java.io.File;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.component.file.GenericFile;
public class MyProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
Message inMsg = exchange.getIn();
Object body = inMsg.getBody();
if (body instanceof File) {
System.out.println("Is a FILE");
} else {
System.out.println("Not a FILE");
}
if (body instanceof GenericFile) {
System.out.println("Is a GF for sure");
GenericFile gf = (GenericFile) body;
String fileName = gf.getFileName();
System.out.println("Filename: " + fileName);
} else {
System.out.println("NOT a GF");
}
}
}
Router
package com.javainuse.route;
import org.apache.camel.builder.RouteBuilder;
import com.test.processor.MyProcessor;
public class SimpleRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
// Transfer files from one another using a processor
from("file:C:/inputFolder?noop=true")
.process(new MyProcessor())
.to("file:C:/outputFolder")
.setBody().simple("Test")
.log("Test log");
}
}
I am using minikube and run the command:
kamel run SimpleRouteBuilder.java --dev
[1] Exception in thread "main" org.apache.camel.RuntimeCamelException: org.joor.ReflectException: Compilation error: /com/test/route/SimpleRouteBuilder.java:4: error: package com.test.processor does not exist
[1] import com.test.processor.MyProcessor;
This is expected as camel-k does not know where to find the classes for your processor so you have two options:
embed the processor as inner class of your route
package your processor as a maven artifact (you can also use jitpack to avoid having to publish it to a maven repo while testing) and list it as any other dependency

How to use system environment variables instead configuration.properties in automation framework?

I have created a WebApp test automation framework for my project on my local machine using Maven, Java and TestNG frameworks. We have TeamCity in our CI/CD and my team would like the framework to use TeamCity's environment variables (including username and pass to the WebApp under test) instead porperties hard coded in my projects 'configuration.properties' file.
The mechanism used in my project when it comes to properties configuration is as follows:
- resources package which contains 'configuration.properties' file. This file contains variables such as app.url, browser, chrome.driver.location.
- 'configuration.properties' file is then read by classes in configuration package. Classes are as follows: AppProperties.java, ConfigurationProperties.java, PropertiesLoader.java, LocalWebDriverPorperties.java and TestRunProperties .java classes
Configuration.properties
app.url=url for app under test
is.remote.run=false
grid.url="";
browser=CHROME
chrome.driver.location=C:/Selenium/chromedriver.exe
firefox.driver.location=C:/Selenium/geckodriver.exe
AppProperties.java
package configuration;
public class AppProperties {
public static String getAllUrl() {
return ConfigurationProperties.getProperties().getProperty("app.url");
}
}
ConfigurationProperties.java
package configuration;
import java.util.Properties;
public class ConfigurationProperties {
private static Properties properties;
private ConfigurationProperties() {
}
public static void setProperties(Properties properties) {
ConfigurationProperties.properties = properties;
}
public static Properties getProperties() {
if (properties == null) {
throw new IllegalStateException("Please set properties using setProperties() before calling getProperties()");
}
return properties;
}
}
PropertiesLoader.java
package configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoader {
private Logger logger = LogManager.getLogger(PropertiesLoader.class);
public Properties getPropertiesFromFile(String propertiesFileName) {
InputStream inputStream = null;
Properties properties = new Properties();
try {
logger.info("Trying to load properties with file name: " + propertiesFileName);
inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFileName);
if (inputStream != null) {
properties.load(inputStream);
logger.info("Successfully loaded properties for file: " + propertiesFileName);
} else {
throw new FileNotFoundException("Property file '" + propertiesFileName + "' not found in the classpath");
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Cannot load properties due to IOException!");
} finally {
closeResource(inputStream);
}
return properties;
}
private void closeResource(InputStream inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
LocalWebDriverProperties.java
package configuration;
public class LocalWebDriverProperties {
return BrowserType.valueOf(ConfigurationProperties.getProperties().getProperty("browser"));
// }
public static String getChromeWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("chrome.driver.location");
}
public static String getFirefoxWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("firefox.driver.location");
}
public static String getInternetExplorerWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("ie.driver.location");
}
}
TestRunProperties.java
package configuration;
import driver.manager.BrowserType;
public class TestRunProperties {
public static String getGridUrl() {
return ConfigurationProperties.getProperties().getProperty("grid.url");
}
public static BrowserType getBrowserToRun() {
return BrowserType.valueOf(ConfigurationProperties.getProperties().getProperty("browser"));
}
public static boolean getIsRemoteRun(){
return Boolean.parseBoolean(ConfigurationProperties.getProperties().getProperty("is.remote.run"));
}
}
The above configuration works fine if i specify server properties like (app.url, browser) used by Team City server in configuration.properties file of the framework.
But what i am looking for is to use a different approach and instead of having properties set up in configuration.properties file- use an environment variables of TeamCity.
Probably having this functionality in one class like AppProperties.java instead of using 5 classes as I am doing at the moment.
Is there any way to do this?

NoClassDefFoundException while trying to use HikariCP [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 6 years ago.
I'm so noob at external stuff to Bukkit programming, so I'm sorry if it's so easy to solve :P
I have a problem, and it's that when I try to use HikariCP in my project, it returns in an error (the title one).
I'm using it in a BungeeCord plugin.
The weird thing is that I have done this successfully couples of times, and I don't know why it isn't working this time.
The error / log:
06:13:36 [ADVERTENCIA] Exception encountered when loading plugin: DiverseReport java.lang.NoClassDefFoundError: com/zaxxer/hikari/HikariDataSource at net.srlegsini.DiverseReport.Bungee.MClass.onEnable(MClass.java:44) at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227) at net.md_5.bungee.BungeeCord.start(BungeeCord.java:272) at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:55) at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15) Caused by: java.lang.ClassNotFoundException: com.zaxxer.hikari.HikariDataSource at net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53) at net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27) at java.lang.ClassLoader.loadClass(Unknown Source) ... 5 more
My main class:
package net.srlegsini.DiverseReport.Bungee;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import com.zaxxer.hikari.HikariDataSource;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import net.srlegsini.DiverseReport.Bukkit.UUIDFetcher;
public class MClass extends Plugin {
static Configuration config;
static MClass plugin;
static HikariDataSource hikari;
static Connection connection;
public void onEnable() {
BungeeCord.getInstance().getPluginManager().registerListener(this, new ChannelListener());
BungeeCord.getInstance().registerChannel("Return");
loadCfg();
if (!config.contains("MySQL")) {
config.set("MySQL.Enable", false);
config.set("MySQL.Host", "localhost");
config.set("MySQL.Port", 3306);
config.set("MySQL.User", "user");
config.set("MySQL.Pass", "pass");
config.set("MySQL.Database", "Sr_DiverseReport");
}
saveCfg(getDataFolder());
hikari = new HikariDataSource();
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("serverName", config.getString("MySQL.Host"));
hikari.addDataSourceProperty("port", 3306);
hikari.addDataSourceProperty("databaseName", config.getString("MySQL.Database"));
hikari.addDataSourceProperty("user", config.getString("MySQL.User"));
hikari.addDataSourceProperty("password", config.getString("MySQL.Pass"));
try {
Class.forName("com.mysql.jdbc.Driver");
connection = hikari.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
}
saveCfg(getDataFolder());
}
public void loadCfg() {
try {
File file = new File(getDataFolder(), "config.yml");
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
if (!file.exists()) {
file.createNewFile();
}
config = ConfigurationProvider.getProvider(YamlConfiguration.class)
.load(new File(getDataFolder(), "config.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveCfg(File dataFolder) {
try {
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(dataFolder, "config.yml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#SuppressWarnings({ "unused", "deprecation" })
public static String getUUID(String playerName) {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList("evilmidget38", "mbaxter"));
String playerUUID = null;
try {
playerUUID = UUIDFetcher.getUUIDOf(playerName).toString();
} catch (Exception e2) {
playerUUID = BungeeCord.getInstance().getPlayer(playerName).getUniqueId().toString();
}
return playerUUID;
}
}
My procedure:
Create the project, import BungeeCord.jar, HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar in buildpath, import HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar
It worked in other projects, but magically, it's broken.
I don't want to use Maven, just because it must have a fix, because as I said, I used this same procedure so many times in the past.
Thank you for taking the time to read this :)
EDIT:
Image of the project
It's all in the exception:
Caused by: java.lang.ClassNotFoundException: com.zaxxer.hikari.HikariDataSource
The HikariDataSource is missing at runtime, you need to provide it somehow, for example by copying the relevant .jar with 'drivers' into your server libraries folder.
Also see some related questions:
How to set up datasource with Spring for HikariCP? and
How do I configure HikariCP in my Spring Boot app in my application.properties files?
From the exception it is clear that HikariCP-2.6.0.jar was in classpath during compile time but is missing in runtime and from the image of the project structure, it is also clear that both HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar are missing as library reference in the ide. You need to keep these jar in your classpath library during compile time and runtime.

Property file is not found and throwing exception?

I am trying to read properties file to fetch the values. But, the code is throwing an exception.
Exception
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cisco.installbase.hiveconnector.ReadProperties.getInstance(ReadProperties.java:28)
at com.cisco.installbase.hiveconnector.MainApp.main(MainApp.java:7)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:16)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:12)
at com.cisco.installbase.hiveconnector.ReadProperties$PropHolder.<clinit>(ReadProperties.java:23)
... 2 more
ReadProperties.java
package com.cisco.installbase.hiveconnector;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class ReadProperties {
private final Properties props = new Properties();
private ReadProperties()
{
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
try{
props.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
private static class PropHolder{
private static final ReadProperties INSTANCE = new ReadProperties();
}
public static ReadProperties getInstance()
{
return PropHolder.INSTANCE;
}
public String getProperty(String key)
{
return props.getProperty(key);
}
public Set<String> getAllPropertyNames()
{
return props.stringPropertyNames();
}
public boolean containsKey(String key)
{
return props.containsKey(key);
}
}
the directory structure and location of my prop file
Can someone help me with the location where the property file needs to be put.
Your file config.properties is not on classpath, therefore cannot be loaded via this.getClass().getClassLoader().getResourceAsStream("config.properties")
Put it under src/main/resources
Please consult the Standard Maven directory layout
If this is a typical maven project then the properties file goes under src/main/resources. Maven will move it into the classpath for you.

Velocity can't find resource

Something is wrong and it is very frustrating. I read on velocity's homepage that when I run a webapp then some properties should be set. And I've done that but no matter what I do I keep getting the same error.
This is where I set the props and use velocity
public class ConfirmationMailGenerator implements MailGenerator {
private BasicUser user;
private String htmlTemplate = "HTMLConfirmationMailTemplate.vsl";
private String plainTemplate = "PlainConfirmationMailTemplate.vsl";
public ConfirmationMailGenerator(BasicUser user) {
this.user = user;
}
public StringWriter generateHTML() throws Exception {
Properties props = new Properties();
props.setProperty("resource.loader", "wepapp");
props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
props.setProperty("webapp.resource.loader.path", "/WEB-INF/mailtemplates/");
VelocityEngine engine = new VelocityEngine(props);
VelocityContext context = new VelocityContext();
engine.init();
Map map = createDataModel();
context.put("user", map);
Template template = engine.getTemplate(htmlTemplate);
StringWriter writer = new StringWriter();
template.merge(context, writer);
return writer;
}
...
}
The files is of course saved in /WEB-INF/mailtemplates/.
If I use this I get this error:
SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
SEVERE: The log message is null.
Thank you for your time:)
You are using the Webapp resourceloader, which is intended for pages served by the Velocity Tools servlet. (It requires some special initialization to find the root of the servlet context).
I recommend you use the ClasspathResourceLoader, then put the files into WEB-INF/classes, or elsewhere in your classpath. This is really the most straight forward approach.
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
More info is here:
https://velocity.apache.org/engine/1.7/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html
Will Glass answer is correct, but the configuration should be:
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
Note the class at the beginning of the second line. See the links provided by him for more details!.
Note: Making an answer instead of a comment due to privileges.
Velocity is probably using the class loader to find those files. I'd recommend putting them in WEB-INF/classes, which is in the CLASSPATH by default.
I am fine it as follow,
In velocity.properties file
resource.loader=class, file
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path=vm_template
runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
runtime.log.logsystem.log4j.category=velocity
input.encoding=UTF-8
output.encoding=UTF-8
And at my java class
import java.io.StringWriter;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.EscapeTool;
import org.apache.velocity.tools.generic.LoopTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.apache.velocity.tools.generic.SortTool;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class VelocitySupport implements InitializingBean {
private static Logger log = Logger.getLogger(VelocitySupport.class);
#Autowired private Properties properties;
public final void afterPropertiesSet() throws Exception {
location = location.replace("classpath:", "");
Resource res = new ClassPathResource(location);
Properties prop = new Properties();
prop.load(res.getInputStream());
String staticDir = System.getProperty("staticDir");
String tempPath = prop.getProperty("file.resource.loader.path");
tempPath = staticDir + "/" + tempPath;
prop.setProperty("file.resource.loader.path", tempPath);
Velocity.init(prop);
}
public static String merge(final String template, final VelocityContext vc) throws Exception {
try {
vc.put("date", new DateTool());
vc.put("escape", new EscapeTool());
vc.put("math", new MathTool());
vc.put("number", new NumberTool());
vc.put("iterate", new LoopTool());
vc.put("sort", new SortTool());
Template temp = Velocity.getTemplate(template);
StringWriter sw = new StringWriter();
temp.merge(vc, sw);
sw.flush();
return sw.toString();
}
catch (ResourceNotFoundException e) {
log.error("", e);
throw e;
}
catch (ParseErrorException e) {
log.error("", e);
throw e;
}
}
private String location;
public final void setLocation(final String location) {
this.location = location;
}
}
And insert VM arguments of project as follow..
-DstaticDir= "your directory for template path"
That may be helpful for you...
For resolving this error
--WEB-INF/classes and all the JARs in WEB-INF/lib are in the CLASSPATH. Try moving your folder with the .vm files under WEB-INF/classes
--dont put the abolute path eg. if abc.vm file is in /public_html/WEB-INF folder then put path = "/public_html/WEB-INF/abc.vm" for velocity template path.

Categories