Runnig spring via Daemon as Standalone - java

I am trying to run Spring via Daemon service as stand alone application ofcourse.
I have configured run.sh script and added to it all Spring jars framework.
Now i am trying to execute my starting point class from the Daemon classes this way:
Code:
public class FeedDaemon implements Daemon
{
public FeedDaemon()
{
}
protected final static Logger log = LoggerFactory.getLogger(FeedDaemon.class);
protected boolean shouldBeRunning = false;
protected ProviderFactory runner = null;
#Override
public void destroy()
{
runner = null;
}
#Override
public void init(DaemonContext arg0) throws Exception
{
runner = new ProviderFactory();
}
public void start() throws RuntimeError, ConfigError
{
log.info("Starting daemon");
runner.start();
}
public void stop() throws Exception
{
log.info("Starting Shutting daemon ...");
runner.stop();
}
}
Code:
package com.spring.test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.aspect.Spring3HelloWorld;
import com.spring.beans.ParkingCar.CarBean;
import com.spring.beans.ParkingCar.CarMaker;
import com.spring.beans.ParkingCar.FourWheelsVechile;
import com.spring.beans.ParkingCar.TwoWheelsVechile;
import com.spring.beans.ParkingCar.Vechile;
import com.spring.beans.ParkingCar.VechileDetails;
import com.spring.beans.calculator.CalculateNumbersHolderBean;
import com.spring.beans.calculator.CalculateStrategyBean;
import com.spring.beans.calculator.CalculatorBean;
public class Spring3HelloWorldTest
{
static Logger logger = Logger.getLogger(Spring3HelloWorldTest.class);
public static void execute()
{
try
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
logger.debug("3");
Vechile fourWheelsVechile = (FourWheelsVechile) context.getBean("Ambulance");
fourWheelsVechile.drive();
CarMaker carMaker = (CarMaker) context.getBean("carMaker");
CarBean carBean = carMaker.createNewCar();
carBean.driveCar();
}
catch (Throwable e)
{
logger.error(e);
}
}
}
And I get this error:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:164)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at com.spring.runner.FeedDaemon.init(FeedDaemon.java:37)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 6 more
Cannot load daemon
Service exit with a return value of 3
thats my run.sh script:
export JAVA_HOME=/../
FIXGW=/../FIXGW
CLASSPATH=$FIXGW/lib/FeedHandler.jar:$FIXGW/lib/FixSpring.jar:$FIXGW/lib/org.springframework.web-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.web.struts-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.web.servlet-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.web.portlet-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.test-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.orm-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.jms-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.jdbc-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.context.support-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.aspects-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.transaction-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.oxm-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.instrument-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.instrument.tomcat-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.expression-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.core-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.context-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.beans-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.asm-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.aop-sources-3.1.1.RELEASE.jar:$FIXGW/lib/commons-daemon-1.0.3.jar
cd $FIXGW
/../jsvc -user fox \
-XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:SurvivorRatio=16 \
-Dlog4j.configuration=file:$FIXGW/conf/log4j.properties \
-outfile /dev/null \
-errfile $FIXGW/logs/error.log \
-verbose -classpath $CLASSPATH \
com.spring.runner.FeedDaemon
Any idea?
thanks,
ray.

You seem to have added all the jars containing the Spring java source (uncompiled code). You should instead add the jars not named ...-sources... to the class path.

spring jars are not in classpath. Check this my answer. Add all libraries in classpath via command line(ofcourse in your shell script).
Well I see your problem now. You have source files in classpath. Those are mentioned as org.springframework.web.servlet-sources-3.1.1.RELEASE.jar while compiled jars are mentioned as org.springframework.web.servlet-3.1.1.RELEASE.jar

Related

java "launch" returns "java.lang.ClassNotFoundException"

when i try to run my java file with javafx from command prompt im returned with a error, the error says the problem of the code is coming from a java launch command which i dont know how to fix. Please give a fix.
the error:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: Main1
at javafx.graphics#19/javafx.application.Application.launch(Application.java:314)
at Main1.main(Main1.java:47)
Caused by: java.lang.ClassNotFoundException: Main1
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:488)
at java.base/java.lang.Class.forName(Class.java:467)
at javafx.graphics#19/javafx.application.Application.launch(Application.java:302)
at Main1.main(Main1.java:47)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:421)
at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
code:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.web.*;
import javafx.scene.image.*;
public class Main1 extends Application {
public void start(Stage stage)
{
try {
stage.setTitle("Fierce Pcs");
WebView w = new WebView();
WebEngine e = w.getEngine();
e.load("https://www.example.com/");
stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png")));
Scene scene = new Scene(w, w.getPrefWidth(),
w.getPrefHeight());
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String args[])
{
launch(args);
}
}
the command which i give the command prompt:
java --module-path %PATH_TO_FX% --add-modules javafx.fxml,javafx.web C:\Users\example\OneDrive\Desktop\Main1.java
Try this:
launch(Main1.class, args);
Or stay with launch(args) and compile your Main1.java to Main1.class and run (i suppose this is the default (none) package):
javac --module-path %PATH_TO_FX% --add-modules javafx.fxml,javafx.web Main1.java
java --module-path %PATH_TO_FX% --add-modules javafx.fxml,javafx.web Main1

Setting pkcs11 cfg file

I am trying to run an example of how to use pkcs11 using the next code
import java.lang.reflect.Constructor;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;
public class SignWithPKCS11
{
public static void main (String [] args)
{
try
{
Class<?> pkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
Constructor<?> construct = pkcs11Class.getConstructor(new Class[] {String.class});
String configName = "pkcs11.cfg";
Provider p = (Provider)construct.newInstance(new Object[] {configName});
Security.addProvider(p);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}
The code above, calls the file pkcs11.cfg. The content of it is
name = JSignPdf
library = /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
using this command
java -cp . SignWithPKCS11
java -Djava.library.path=".:/home/grados-sanchez/grive/E-CONNECTING/david" -cp . SignWithPKCS11
but I get
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at SignWithPKCS11.main(SignWithPKCS11.java:32)
Caused by: java.security.ProviderException: Initialization failed
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:376)
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
Caused by: java.io.IOException: libopensc.so.6: cannot open shared object file: No such file or directory/home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
Could you help me, to fix it, please? What is wrong?
NOTE: When I did:
ls -l /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
I get
-rw-rw-r-- 1 user user 220232 Jun 19 01:59 /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
It seems that it's not the opensc-pkcs11.so that is missing, but a dependency of that library. Use ldd to check those dependencies. If they reside in the same directory as the provider, you need to set LD_LIBRARY_PATH=/home/grados-sanchez/grive/E-CONNECTING/david before executing the command (as a prefix, for example).

Getting NullPointerException while running a normal Mapreduce program in Eclipse

I am getting NullPointerException when trying to execute a simple MapReduce program. I am unable to understand where the problem is?
package MapReduce.HadMapReduce;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class RecCount extends Configured implements Tool {
public int run(String[] arg0) throws Exception {
Job job = Job.getInstance(getConf());
FileInputFormat.setInputPaths(job, new Path("C:\\singledeck.txt"));
FileOutputFormat.setOutputPath(job, new Path("C:\\temp123"));
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String args[]) throws Exception {
System.exit(ToolRunner.run(new RecCount(), args));
}
}
Error is:
Exception in thread "main" java.lang.NullPointerException
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
at org.apache.hadoop.util.Shell.runCommand(Shell.java:483)
at org.apache.hadoop.util.Shell.run(Shell.java:456)
at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:722)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:815)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:798)
at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:731)
at org.apache.hadoop.fs.RawLocalFileSystem.mkOneDirWithMode(RawLocalFileSystem.java:489)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirsWithOptionalPermission(RawLocalFileSystem.java:530)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:507)
at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:305)
at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:133)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:144)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
at MapReduce.HadMapReduce.RecCount.run(RecCount.java:22)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:84)
at MapReduce.HadMapReduce.RecCount.main(RecCount.java:26)
This is the logic which is happening behind the scenes:
ToolRunner is calling the below run method and this method calls its other run method (which is pasted right below this) where the configuration is set if it is null.
public static int run(Tool tool, String[] args) throws Exception {
return run(tool.getConf(), tool, args);
}
public static int run(Configuration conf, Tool tool, String[] args) throws Exception {
if (conf == null) {
conf = new Configuration();
}
GenericOptionsParser parser = new GenericOptionsParser(conf, args);
// set the configuration back, so that Tool can configure itself
tool.setConf(conf);
// get the args w/o generic hadoop args
String[] toolArgs = parser.getRemainingArgs();
return tool.run(toolArgs);
}
In the last statement above run method is called which I implemented because I implemented Tool interface. I don't see any error in my code. Please let me know if you can find any!
Can someone please explain what is the problem with my code?

Weird NoClassDefFoundError of Java class

Environment: jdk1.7
javax.servlet-api-3.0.1.jar is needed for this test.
Reproduce steps:
javac Test1.java -cp javax.servlet-api-3.0.1.jar build Test1.java with javax.servlet-api-3.0.1.jar
javac Test2.java -cp javax.servlet-api-3.0.1.jar build Test2.java with javax.servlet-api-3.0.1.jar
javac Test3.java build Test3.java
java -classpath .:javax.servlet-api-3.0.1.jar Test3 run Test3 with dependency. Following is the output. It's OK here.
hello world1
hello world2
But when this command java Test3 run, Exception is thrown. The result at the end of this post. The weird thing is that "hello world1" could be printed out, but instead of printing out "hello world2" an exception is thrown.
Test1.java
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class Test1 {
public void getRequest(HttpServletResponse resp) throws IOException {
OutputStream os = resp.getOutputStream();
resp.getOutputStream().close();
}
public void hello(String world) {
System.out.println("hello " + world);
}
}
Test2.java
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class Test2 {
public void getRequest(HttpServletResponse resp) throws IOException {
OutputStream os = resp.getOutputStream();
try {
resp.getOutputStream().close();
} catch (Exception e) {
}
}
public void hello(String world) {
System.out.println("hello " + world);
}
}
Test3.java
public class Test3 {
public static void main(String[] args) {
new Test1().hello("world1");
new Test2().hello("world2");
}
}
output of the final step. Test2.hello("world2") throw an exception:
hello world1
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
at cn.test.abc1.Test3.main(Test3.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletOutputStream
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
I am very confused with the Exception. Because I didn't use any code in the Class ServletOutputStream. And the difference of the Test1 and Test2 is only a try blocked.
This Question is not a duplicated question as it marked. Because JVM should not throw an Exception when a try block get involved.
After compare the output of javap -v -c Test?, I get the answer.
The javac create StackMapTable attribute for try/catch block of Test2.java.
If StackMapTable is found on class loading, JVM will perform bytecode verification and check referenced class. That's why it throw java.lang.NoClassDefFoundError.
https://stackoverflow.com/a/25110513 describe more detail about StackMapTable.
Old wrong answer
I compile the Test1.java and decompile it, the source code become:
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
public class Test1 {
public void getRequest(HttpServletResponse resp) throws IOException {
ServletOutputStream os = resp.getOutputStream();
resp.getOutputStream().close();
}
public void hello(String world) {
System.out.println("hello " + world);
}
}
Since the return object of resp.getOutputStream() is javax.servlet.ServletOutputStream. JVM still have to verify this class is sub-class of java.io.OutputStream before case it at runtime, so it try to load ServletOutputStream.class. But JVM cannot find it and throw ClassNotFoundException.
Makes perfect sense to me. Test3 references Test2 and Test1 and both need javax.servlet-api-3.0.1.jar on the classpath at runtime.

Classic error, NoClassDefFoundError

Classic problem, NoClassDefFoundError what do I need? create new class?
import java.net.*;
import java.io.*;
import org.xsocket.*;
import org.xsocket.connection.*;
import java.io.IOException;
public class SocketClient {
public static void main(String[] args) {
try {
IBlockingConnection bc = new BlockingConnection("127.0.0.1", 8090);
String req = "Hello server";
bc.write(req + "\r\n");
} catch (IOException e){}
System.out.println("missing");
}
}
I get the following exception:
C:\Users\Wildfire\Desktop>java -cp xSocket-2.8.14.jar SocketClient
Exception in thread "main" java.lang.NoClassDefFoundError: SocketClient
Caused by: java.lang.ClassNotFoundException: SocketClient
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: SocketClient. Program will exit.
This should work:
java -cp xSocket-2.8.14.jar SocketClient
("cp" is short for "classpath")
java -cp xSocket-2.8.14.jar;. SocketClient
You are missing the current directory from the class path.
For completeness (thanks to Mark):
on Unix/Linux/etc...:
java -cp xSocket-2.8.14.jar:. SocketClient
Your classpath is probably wrong. Try:
java -classpath dir_missing_from_class_path your_java_file

Categories

Resources