When I imported written code using Eclipse and run it as java application, I get an Exception during run ready code
This is the error...
Exception in thread "main" java.lang.NoSuchMethodError: backtype.storm.topology.TopologyBuilder.setBolt(Ljava/lang/String;Lbacktype/storm/ topology/IBasicBolt;Ljava/lang/Integer;)Lbacktype/storm/topology/BoltDeclarer;
at TopologyMain.main(TopologyMain.java:18)
Code is
import spouts.WordReader;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.tuple.Fields;
import bolts.WordCounter; import bolts.WordNormalizer;
public class TopologyMain { public static void main(String[] args) throws InterruptedException {
//Topology definition
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word-reader",new WordReader());
builder.setBolt("word-normalizer", new WordNormalizer()).shuffleGrouping("word-reader");
builder.setBolt("word-counter", new WordCounter(),1).fieldsGrouping("word- normalizer", new Fields("word"));
//Configuration
Config conf = new Config();
conf.put("wordsFile", args[0]); onf.setDebug(false);
//Topology run
conf.put(Config.TOPOLOGY_MAX_SPOUT_PENDING, 1);
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("Getting-Started-Toplogie", conf, builder.createTopology());
Thread.sleep(1000); cluster.shutdown();
}
}
Probably you've modified your TopologyBuilder class or JAR having that class. Because that kind of error implies that your compile-time and run-time versions of classes are not the same, i.e. the .class has been somehow modified after you compiled your program. Particularly, the program was compiled with setBolt() method, and after that, the method was removed, renamed or modified within .class
The program and method arguments that you are passing as builder.setBolt("word-counter", new WordCounter(),1).fields...() is correct. The method signature builder.setBolt(String id, IBasicBolt bolt, Number parallelism_hint) should accept the parameters that you are passing as long as the JDK version set is 1.5 or above.
Starting from the Java version 1.5 Autoboxing feature, the imported .class or compiled class should resolve the references from primitive int to java.lang.Integer to java.lang.Number as java.lang.Number is super class.
The main issue could be the JDK version 1.4 or less set in your project or workspace settings. Changing the JDK version to 1.5 or above should resolve the issue.
add following maven dependency
<dependency>
<groupId>storm</groupId>
<artifactId>storm</artifactId>
<version>0.8.2</version>
</dependency>
Related
I need to use a .jar library, given by my teacher, to code for my Java class.
I am using VS Code, with the Java Extension Pack installed, for Java Project Management.
Can someone please explain me step by step how is it possible to import the .jar library, in order to use the classes defined by my teacher.
I have tried to copy the .jar in the lib folder and then add the reference, but it still did not work. I also know that I have to declare the classpath, but when I create the Java Project the .classpath file is not created automatically.
Thanks already!
First you should examine the classes in .jar file. Then you should load that class as,
Class<?> c1 = Class.forName("java.lang.String");
Then after you can use that class by calling that Class reference type variable.
See this example as well,
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
// get the Class instance using forName method
Class c1 = Class.forName("java.lang.String");
System.out.print("Class represented by c1: "+ c1.toString());
} }
Try to understand the code and implement proper solution to your project.
Good Luck.
I have included all the necessary log4j jar files and I don't understand why I'm still getting this error.
package in.gstzen.einvoice;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class Log4jExample {
private static Logger logger = LogManager.getLogger(Log4jExample.class);
public static void main(String[] args) throws Exception {
System.out.println("Hello World!\n");
logger.debug("Log4jExample: A Sample Debug Message");
logger.info("Log4jExample: A Sample Info Message");
logger.warn("Log4jExample: A Sample Warn Message");
logger.error("Log4jExample: A Sample Error Message");
logger.fatal("Log4jExample: A Sample Fatal Message");
System.out.println("Completed...");
}
}
This looks like a conflict between log4j versions 1 and 2. Your class is importing version 1 classes, but the error message is in response to the spring framework not finding the jar files for version 2 classes. You want to use one version or the other.
Version 1 is going to have classes with classpath org.apache.log4j.x
The jar will be called log4j-1.x.x
Version 2 is going to have classes with classpaths like org.apache.logging.log4j.x
The jars will be named log4j-core-2.x.x and an implementation like log4j-slf4j18-impl-2.x.x
See this migration helper: https://logging.apache.org/log4j/2.x/manual/migration.html
Note: If your classpath includes both the version 1 and version 2 jars resolution of the actual classes used can be somewhat random (and problematic).
Please list the jars you added.
See here:
https://www.cwiki.us/questions/13469089/could-not-initialize-class-org.apache.logging.log4j.logmanager
I'm simply try to run this code:
import com.sun.rowset.CachedRowSetImpl;
public class Test {
public static void main(String[] args) throws Exception{
CachedRowSetImpl crs = new CachedRowSetImpl();
}
}
When I run it I get:
Error:(1, 15) java: package com.sun.rowset is not visible (package
com.sun.rowset is declared in module java.sql.rowset, which does not
export it)
I'm using IntelliJ and I tried to import rs2xml.jar, and that still doesnt help.
As of Java 9, you can not access this class directly. And in the ideal way you shouldn't do that. That is because this class's package is not exported in the module javax.sql.rowset.
The proper way to do that in Java 9+ is the use of RowSetProvider to access a RowSetFactory to produce an instance of an implementation of CachedRowSet
import javax.sql.rowset.*;
public class Test {
public static void main(String[] args) throws Exception {
CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
}
}
To understand that we can go to the module description (module-info.java) and find a list of exported packages:
exports javax.sql.rowset;
exports javax.sql.rowset.serial;
exports javax.sql.rowset.spi;
This should be used with Java 10
Instead of
CachedRowSet crs = new CachedRowSetImpl();
use
CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
In addition to the answers here, it is important to note that you should never use com.sun.rowset.CachedRowSetImpl, even in Java 8.
As explained at Are there any good CachedRowSet implementations other than the Sun one? , the RowSetProvider is the standard way to obtain a CachedRowSet.
Packages from sun are internal and subject to change. They should never be used except by JDK developers.
I have a app that executes scala scripts that get passed to it. The following is an example of how I am attempting to execute the scala code.
String script = "println(\"Hello World!\")";
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("scala");
scriptEngine.eval(script);
I get the following error.
[init] error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
Failed to initialize compiler: object scala in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
I am using Java 7 (jdk1.7.0_80.jdk) on a Mac. I am running this from within a spring-boot app.
Scala maven dependency used.
<dependency>
<groupId>org.apache.clerezza.scala</groupId>
<artifactId>script-engine</artifactId>
<version>1.0.0</version>
</dependency>
Why am I getting this error?
We need to import and initialize the script engine like this:
import javax.script.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestScript {
public static void main(String... args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("scala");
...
With ScriptEngineManager().getEngineByName("scala") a matching script engine is looked up. This just gives us the engine but not the required libraries and hooks to really execute a script.
There is one thing to note about the way Scala loads the required standard classes for the JVM. In case of the execution of scala standard Scala libraries are placed in the classpath of the JVM. This would not be the case here. You can read this for a reference. Trying to run a sample class would result in the following exception:
reflect.jar:. TestScript
[init] error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
Failed to initialize compiler: object scala in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
Exception in thread "main" scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
We can work around this by using the scala java tools helping us to load the standard Scala libraries into the JVM classpath. This is one way to make our Scala script work:
import javax.script.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import scala.tools.nsc.interpreter.IMain;
import scala.tools.nsc.settings.MutableSettings.BooleanSetting;
public class TestScript {
public static void main(String... args) throws Exception {
....
((BooleanSetting)(((IMain)engine).settings()
.usejavacp())).value_$eq(true);
....
The other is to simply execute the code with the following option:
$ java -Dscala.usejavacp=true ...
The complete script is this:
import javax.script.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import scala.tools.nsc.interpreter.IMain;
import scala.tools.nsc.settings.MutableSettings.BooleanSetting;
public class TestScript {
public static void main(String... args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("scala");
((BooleanSetting)(((IMain)engine)
.settings().usejavacp()))
.value_$eq(true);
String testScript = "var a:Int = 10";
engine.eval(testScript);
String testScript2 = "println(a)";
engine.eval(testScript2);
String testScript3 = "println(a+5)";
engine.eval(testScript3);
}
}
Compiling and running this is pretty straight forward as we need the Scala libraries in the classpath.
Compiling:
$ javac -cp /Users//hkropp/bin/scala-2.11.7/lib/scala-library.jar:\
/Users//hkropp/bin/scala-2.11.7/lib/scala-compiler.jar:\
/Users//hkropp/bin/scala-2.11.7/lib/scala-reflect.jar \
TestScript.java
Running:
$ java -Dscala.usejavacp=true \-cp /Users//hkropp/bin/scala-2.11.7/lib/scala-library.jar:\
/Users//hkropp/bin/scala-2.11.7/lib/scala-compiler.jar:\
/Users//hkropp/bin/scala-2.11.7/lib/scala-reflect.jar:. \
TestScript
10
15
I want to import and call a java class(which in from an external package ) from a scala object . My code is like this
Java code:
package com.test.services.account;
public class MyMain {
public static void main(String[] args) {
System.out.println("coming into main");
}
}
Scala code:
package com.newtest.newservice.scala
import _root_.com.test.services.account.MyMain
object scalatest {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
// Deployer.main(args)
val de:MyMain = new MyMain()
println(de.toString())
}
}
when i compile it using scalac scalatest.scala, it gives an error
scalatest.scala:2: error: object test is not a member of package com
import root.com.test.services.account.MyMain
^
one error found
Could anybody guide me how can i import my java class into scala code ?
Thanks
Suresh
If you don't want to use something like sbt, you should first decide where your CLASSPATH is. Since you have two different class files (one generated from Java and one from Scala), you need at least one directory where your class files need to live. Let's say that is d:\myclasses.
In that case, you'd compile the java file using this command:
d:\mycode> javac -d d:\myclasses MyMain.java
This would generate your Java class file in the appropriate package structure at d:\myclasses. Then you would compile the scala file like so
scalac -classpath d:\myclasses -d d:\myclasses scalatest.scala
Instead of passing the classpath as part of the scalac command line, you could also set your CLASSPATH environment variable to d:\myclasses.