Eclipse can't find class DefaultSolver of OptaPlanner and throws ClassNotFoundException - eclipse

Hi I'm building a program using optaplanner, and everything seems to be imported and working fine, but when I run and debug the code, I get a ClassNotFoundException "org.optaplanner.core.impl.solver.DefaultSolver". I believe I've imported all the jar files correctly, but just to be sure, what jar file is the DefaultSolver class in?
As a follow up, I keep getting prompted to edit the source lookup path. This happens after running SolverConfig.buildSolver() line 156: "DefaultSolver solver = new DefaultSolver();" I'm running OptaPlanner 6.1.0.Final.

Found the problem, I realized that I had both imported the examples jar file into my buildpath and at one point imported a class from the examples jar file when I was messing around with the examples. Rereading the manual I saw it explicitly said not to import the examples jar file, so I unimported that and found the culprit pretty fast. Thanks for your help.

Both the class SolverConfig and the class DefaultSolver are in the jar optaplanner-core-*.jar.
So this doesn't make sense: if the jar is in the classpath, it should find DefaultSolver. If the jar is not in the classpath, it shouldn't even find SolverConfig. What you're seeing can never happen in a normal classpath.
Are you using anything like OSGi, JBoss modules, jigsaw, Karaff or other classloading magic?

Related

How to solve "Package org is not accessible" error in eclipse while importing org.junit.Assert.*?

I am following a tutorial on unit testing from school and wrote JUnit tests for a method in a class. This is what the files look like : enter image description here
Why am I getting this error? All the annotations like #AfterClass are also underlined with error "cannot resolve #AfterClass to a type" How do I fix this ? I have programmed in Java for a year now but this is my first time using eclipse and writing JUnit test cases.
The JUnit classes are in a library, which is not automatically made available to your project, unless you make it available. For a simple exercise like this, you might have to download the JUnit jar file (https://github.com/junit-team/junit4/wiki/Download-and-Install) , and create a "lib" directory in your project to store it. Then, you add that jar to your project classpath.
If you've done everything right up until this point and are still having issues with your junit imports, your module-info.java file is probably the culprit. If you don't need it, deleting it will silence the IDE complaints.

ClassNotFoundException: com.itextpdf.text.Element

Situation: I have a Java file in my project that uses the features of the iTextPDF library. The project compiles properly. I use JDK 1.7, Tomcat 7.45 and Eclipse Neon.3 Release (4.6.3).
Problem: While starting the server via Eclipse, I get an error:
java.lang.ClassNotFoundException: com.itextpdf.text.Element
What I've tried so far:
Ensured that only 1 version of iTextPdf 5.4.jar is available in the entire project. It's there in WEB-INF/lib folder. It's not there in any of the externally referenced libraries.
I updated my Eclipse.
Any help will be greatly appreciated.
Well, as a starting point- try and expanding the JAR, and see if you can search for or manually find com.itextpdf.text.Element class.
if it's not found there, you know there's nothing wrong with your eclipse or project settings, and nothing wrong with your jar imports.
You should then determine between 3 options:
Is the JAR even on the classpath? it's possible everything is present there, but the project does not even consider looking in it.
Should this class be in the JAR? is it available on other versions of this JAR?
Is this class neccesasry for you application? why is eclipse looking for it, where in the code it is referecened? can you live without it? or, can you manullay replace it with a class file you can find online? (this will take some debug time, and some more research on your part)

How to isolate libraries in an unmanaged dependency .jar file so they don't conflict with others

I need to add a .jar as an unmanaged dependency to an sbt Scala project (it is the java-stellar-sdk). Everything works well as long as I don't run sbt test. There seems to be a Mockito version in the .jar file that conflicts with the one I am using in the project. I get a lot of errors that certain Mockito matchers are not found but everything works fine without the .jar in the lib folder.
Is there a way to tell sbt that it should ignore certain libraries in the .jar or that managed dependencies take precedence? I also found this related question but obviously it didn't help me.
An alternative workaround would also help a lot. Is it possible to isolate the libraries in the jar in a way that allows me to just make a certain package visible to the outside?
Update: The .jar contains Mockito 2 but my project uses Mockito 1, so this is a very simple and obvious conflict, that I can solve by upgrading to Mockito 2 (which I tried and it works). However, the question remains: Is there another reasonable way to isolate the Mockito dependency in the .jar to not interfere with my project in case I can't or don't want to resolve the conflict buy switching to a newer version of the library in question. Maybe altering the .jar to rename the conflicting packages? I don't know. Something like that.
I know that this is a very general question that has likely been discussed somewhere else in depth. However, I didn't find anything that really satisfied me. Links to relevant discussions of the topic are of course appreciated as well.
I can think of 3 ways for you to do it (ordered from simple to difficult):
delete mockito 2 manually from the jar file.
Since the jar is just a zip file, you can extract it, delete all the conflicting files, and pack it again.
compile that jar from source by yourself, and set mockito as a test dependency (as it should be). If you do that, consider opening a PR with your change, to fix the problem for the community
Shade the mockito files in the jar.
shading is the process of renaming all files in a jar file by certain rules. you can either use jarjarlinks or with sbt assembly plugin. see this answer to get you started with sbt assembly: https://stackoverflow.com/a/47974750/245024
You should be able to arrange for your Mockito 1 classes to appear before the Mockito 2 classes on the classpath. That will cause your classes to win any conflicts.

Creating javadoc with gwt-user.jar through Gradle

I'm trying to compile the javadocs for my application, and for some reason it throws errors while compiling like this:
/Users/Sander/.gradle/caches/artifacts-23/filestore/com.google.gwt/gwt-user/2.4.0/jar/949dcb5d14cb0e2c8dec98efc0760be68753c124/gwt-user-2.4.0.jar(com/google/gwt/dom/client/CanvasElement.java):18: error: cannot access Context
import com.google.gwt.canvas.dom.client.Context;
All the errors thrown are found in gwt-user-2.4.0.jar. Basically, what (I think) it's trying to do is compile the .java source files the jar file contains, which it shouldn't do. Is there a way to make the javadoc command ignore java source files?
I've tried removing the source files from the jar manually, this way the build succeeds. However, since the jar is a gradle dependency, manually altering the file is not an option.
I finally got it to work. As it turned out, the javadoc command needed a sourcepath flag set, because otherwise it would start looking for the source files in the classpath, which contained some source files from the GWT jar file. Since the MinimalJavaDocOptions GWT class doesn't support this, I had to set it manually by adding the following line:
options.addStringOption("sourcepath", <path_to_source>)
This fixed the problem. Obviously it is not ideal, a topic in the Gradle Support Forums has already been created, see http://forums.gradle.org/gradle/topics/allow_javadoc_sourcepath_to_be_set_directly

Loading resources off the classpath in Jython using 'classpath:'

I've got a relatively large Java application that would benefit from a bit of Python love. To that end I've been working on getting it up and running in Jython. My current roadblock is getting the classpath right.
I've taken two approaches to setting the classpath:
Using a shell script, I've built up a list of jars and executed java -cp ${CP} -jar jyton.jar where $CP is a list of the jars needed for my app. This doesn't seem to work. I'm unable to import any classes from these jars, getting only ImportError: No module named apache instead.
Using a bootstrap python script, i've created a list of paths using glob, and appended them to the current path using [sys.path.append(path) for path in JAR_LIST]. This seems to work correctly; I can now import any of the classes I need to from the included jars.
The above is a bit confusing as most information I've been able to find has steered towards using $CLASSPATH and -cp to added your jars, but I can't get that to work. So the question so far: Is #2 the proper way to add dependancies to your classpath when using Jython?
The main reason I question my methods is because I'm still having problems fully utilizing my application. A number of places in my app reverences resources using relative URLs : classpath:some-critical-file.xml
some-critical-file.xml and a number of my classes reside within the same jar. I'm able to import classes from that jar, but any attempts to load my xml with classpath:some-critical-file.xml results in a java.io.FileNotFoundException
Any insight as to why my classes are available but relative paths to resources using classpath: are not would be much appreciate. I'm at a loss at this point.
I've run into a little classpath weirdness myself lately. Have you tried the old school approach of:
CLASSPATH = ${CLASSPATH}:your.jar
export CLASSPATH
jython your_script.jy
If you're invoking the standalone jar using java -jar then from the Java Documentation ...
-jar
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
So it it not possible to add anything to the classpath when using -jar. See this answer for a solution - basically add the jython.jar to the classpath (either using -cp or CLASSPATH) and run the org.python.util.jython class directly.