I am having trouble trying to use an imported class from a jar file which is located in the referenced libraries of my project.
So I have a project which has the pydev.jar file in the Referenced Libraries. Pydev.jar contains org.python.pydev.navigator.elements.PythonNode, and I have imported this in one of the Java files. Eclipse does not give an errors when I import and use this in the Java file but when I run the project as an Eclipse application there is a java.lang.NoClassDefFoundError: org/python/pydev/navigator/elements/PythonNode exception being thrown.
Code is trying to cast an ISelection to a PythonNode as below:
IStructuredSelection sel = (IStructuredSelection)
window.getSelectionService().getSelection();
ArrayList<String> testNames = new ArrayList<String>();
Iterator<?> itr = sel.iterator();
String testName = "";
String testSuite = "";
while(itr.hasNext()) {
PythonNode selectionElement = (PythonNode) itr.next();
testName = selectionElement.toString();
testSuite = selectionElement.pythonFile.toString();
testNames.add(testSuite + "." + testName);
}
If anyone can explain why the Exception is being thrown for the use of the PythonNode class at runtime I would be very appreciative. As far as I can see it is imported correctly as it is visible in the Referenced Libraries.
I think you're building either Eclipse RCP or Eclipse plugin. Am I right?
If yes, you should put pydev.jar under plugin dependencies. Go to plugin.xml, Runtime and put pydev.jar in the classpath
Related
I am unable to use swing library with my scala-sdk-2.12.4.
I am using Java 9 version.
When I try to run the program:
package rs.ac.bg.etf.zd173013m.gui
import swing._
object HelloWorld extends SimpleSwingApplication {
def top = new MainFrame {
title = "First Swing App"
contents = new Button {
text = "Click me"
}
}
}
I get the following error:
Exception in thread "main" java.lang.IncompatibleClassChangeError: Method scala.swing.Reactor.$init$()V must be InterfaceMethodref constant
at scala.swing.SwingApplication.<init>(SwingApplication.scala:4)
at scala.swing.SimpleSwingApplication.<init>(SimpleSwingApplication.scala:13)
at rs.ac.bg.etf.zd173013m.gui.HelloWorld$.<init>(Application.scala:5)
at rs.ac.bg.etf.zd173013m.gui.HelloWorld$.<clinit>(Application.scala)
at rs.ac.bg.etf.zd173013m.gui.HelloWorld.main(Application.scala)
You have incompatible JAR versions on your classpath. The code in the JAR containing "SwingApplication" was compiled against a different version of "Reactor" than the one on your classpath.
What are you using to manage your dependencies? I guess that you are downloading them manually.
Switch to a dependency management system like Gradle and this problem should go away, as it will ensure that all your dependencies are consistent.
The error is pointing to EventUnitTesting.readPropertyFile(EventUnitTesting.java:168) in which the body of readPropertyFile() is
private void readPropertyFile() throws IOException, ConfigurationException{
file = new File(fileLocation + unitTestingFileName);
propertiesConfiguration = new PropertiesConfiguration(file);
List<Object> propertyKeysList = propertiesConfiguration.getList("regular");
Iterator<Object> propertyKeysIterator = propertyKeysList.iterator();
regularEvents = new ArrayList<String>();
while(propertyKeysIterator.hasNext()){
regularEvents.add((String)propertyKeysIterator.next());
}
propertyKeysList = propertiesConfiguration.getList("consolidated");
propertyKeysIterator = propertyKeysList.iterator();
consolidatedEvents = new ArrayList<String>();
while(propertyKeysIterator.hasNext()){
consolidatedEvents.add((String)propertyKeysIterator.next());
}
propertyKeysList = propertiesConfiguration.getList("correlated");
propertyKeysIterator = propertyKeysList.iterator();
correlatedEvents = new ArrayList<String>();
while(propertyKeysIterator.hasNext()){
correlatedEvents.add((String)propertyKeysIterator.next());
}
}
whereby I am using the Apache Commons Configuration library version 1.10 to read a properties file that has non-unique keys. I don't receive this error using a JBoss 6.4.8 purpose-built WAR but this error is generating on a JBoss converted 5.2 WAR.
I am using the Apache Commons Lang 2.1 so I'm not sure how org/apache/commons/lang/text/StrLookup can be a problem. All relevant *.java and *.class files have been copied into the converted jar file and everything is fine except this issue.
Note that NoClassDefFoundError is different than ClassNotFoundException. The former can mean that the class was found, but during the a static initializer an exception was thrown.
Wrap this method code in a try catch and output the exception. Likely you will see why.
Looks like some dependent jar was present at compile time but missing at runtime. Can you compare classpaths for build time and runtime. It will give you the difference which jar is missing and causing this issue.
I'm trying to make an Ant build script for my project, which runs fine in Eclipse. When I try to run the exported jar:
java -verbose:class -jar MyProject.jar
I get an unexpected error:
java.lang.NoSuchMethodError: com.employer.MyInterface.myMethod()I
what's really weird about this is debugging attempts like these all appear to be producing expected output (both before the export and after):
System.out.println("1:" + (myObj instanceof MyClass));
System.out.println("2:" + (myObj instanceof MyInterface));
Class c = MyClass.class;
URL myClassURL = c.getProtectionDomain().getCodeSource()
.getLocation();
System.out.println("url: " + myClassURL);
try {
Method[] m = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
System.out.println(m[i].toString());
}
} catch (Throwable e) {
System.err.println(e);
}
System.out.println("gni2:" + myObj.myMethod());
where MyClass implements MyInterface. That is, I can see both the interface and the class in my jar file if I examine it using emacs, I see the class loader load each of them, I see myMethod in getDeclaredMethods, there's only one MyClass.class in the jar, etc.
I've changed this to an Answer your own question because I figured it out while typing up the question. Instead of deleting the question, here's the solution just in case it helps someone else.
The problem ended up being that 2 of the projects that my project depends on have overlapping package+class names. I think a colleague copied code from one project to the other at some point in the past and then only maintained one of them. Eclipse uses the first class it finds based the order in a Build Path setting, whereas Ant overwrites one class with the other based on the order in the build file (and the orders end up being reversed).
I am trying to build a Dynamic web project where user can practice Java code.
I got success on writing the code written by user in a .java file, compile the code & get error messages using Java Compile API.
Now, I need to run JUnit 1.4 Compatible test on that code.
I researched for it, and found something like parameterized junit testing. But my view on how should it be done isn't still clear.
UPDATE
This (http://codingbat.com/prob/p171896) is the exact thing what I'm trying to implement.
I solved this problem with the following steps:
Compile the JUnit TestClass and the ClassToTest
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilerResult = compiler.run(null, null, null,"/path/to/TestClass.java", "/path/to/ClassToTest.java");
System.out.println("Compiler result code: " + compilerResult);
Load the compiled classes into the classloader
File dir = new File("/path/to/class/files/");
URL url = dir.toURI().toURL();
URL[] urls = {url};
ClassLoader classLoader = new URLClassLoader(urls);
Run the tests (make sure that you add junit as a dependency of your project)
Class<?> junitTest = Class.forName("TestClass", true, classLoader);
Result result = junit.run(junitTest);
Edit: If you're not needing the JUnit tests to be dynamic you can skip the compilation of those tests and add them here: junit.run(JunitTest.class)
I would like to create a Java project from another Java project, using some script or Java methods from an Eclipse library, whether it exists. An alternative to this can be duplicating a previously manually-created project. Is there any approach to this?
Thanks.
I believe you can make use of IProject#copy (inherited from IResource.copy)
Adding to Alexander Pavlov's answer, I found that a little extra work is required to copy the project properties (such as referenced projects) in addition to just copying the project files.
public static IProject copyProject(String projectName) throws CoreException {
IProgressMonitor m = new NullProgressMonitor();
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject project = workspaceRoot.getProject(projectName);
IProjectDescription projectDescription = project.getDescription();
String cloneName = projectName + "_copy";
// create clone project in workspace
IProjectDescription cloneDescription = workspaceRoot.getWorkspace().newProjectDescription(cloneName);
// copy project files
project.copy(cloneDescription, true, m);
IProject clone = workspaceRoot.getProject(cloneName);
// copy the project properties
cloneDescription.setNatureIds(projectDescription.getNatureIds());
cloneDescription.setReferencedProjects(projectDescription.getReferencedProjects());
cloneDescription.setDynamicReferences(projectDescription.getDynamicReferences());
cloneDescription.setBuildSpec(projectDescription.getBuildSpec());
cloneDescription.setReferencedProjects(projectDescription.getReferencedProjects());
clone.setDescription(cloneDescription, null);
return clone;
}