Could not locate Clojure resource on classpath - java

I am trying to access some of my clojure functions in my Java code (I am using Eclipse and CounterClockWise). Firstly, I was not sure how to do it? But then I found some answer here.
I am using clojure.lang.RT package for calling .clj files in my Java code. While naming namespaces, .clj files proper conventions has been followed like ns test.clojure.core is in test.clojure package where-in core is .clj file. Also upon following some information, I have added java-source-path (i.e. java-source-path src/test/java) and source-path (i.e. source-path src/test/clojure) in my project.clj.
But, when I run my Java file following error is given - a) Could not locate Clojure resource on classpath b) Attempting to call unbound fn.
Please if anyone in community could help me (assuming I am absolute beginner) through detailed procedure or tutorial I would be grateful, as only information I could get (for using clojure.lang.RT) is very little.
Looking forward to get a complete answer to end my frustration.

I guess you are using RT.loadResourceScript which is causing the proble.
You can try out something like this:
//Load the namespace
RT.var("clojure.core","eval").invoke(RT.var("clojure.core","read-string").invoke("(use 'test.clojure.core)"));
//Find a function in namespace
IFn fn = (IFn)RT.var("test.clojure.core","hello");
//Call that function
fn.invoke();

Related

Jython ImportError: No module named gargoylesoftware, no resolution found

I would like to use jython for basic web scraping task rather than learning java. To learn the basics I'm using an example from http://blog.databigbang.com/web-scraping-ajax-and-javascript-sites/ I've been unsuccessfully trying to run the gartner.py code from Windows cmd. Could anyone suggest a resolution to why both
jython -J-classpath "path\to\the\jars\*" path\to\gartner.py
and
jython path\to\gartner.py
keep on throwing out
Traceback (most recent call last):
File "path\to\gartner.py", line 1, in <module>
import com.gargoylesoftware.htmlunit.WebClient as WebClient
ImportError: No module named gargoylesoftware
given I've got environment variables set up for jython path\to\jython\bin, for java path\to\Java\jdk-14.0.1\bin and for the htmlunit-2.40.0 I've added path\to\jars\htmlunit-2.40.0\lib to the CLASSPATH.
I understand that jython should pick up the specified package in jython -J-classpath "path\to\the\jars\*" path\to\gartner.py but it does not find it. Also, I understand that in the case of jython path\to\gartner.py the defined CLASSPATH variable is available to Java pointing at htmlunit-2.40.0 (as mentioned above) whilst jython serves only as a translator from python to java. So - in my understanding - java shouild have all parameters available to import the desired module. Please, could anyone confirm?
I appreciate this subject has been somewhat discussed but there is no clear resolution available. What could I be missing?
The error looks very clearly like you're missing a Java dependency. The jython issue with this specific library has already been discussed in a different thread: instantiating a webclient object in jython giving strange results

library for Java/Groovy code analysis

I have a project that should analyze Java and Groovy code and build a special script from the source code.
Googling code analysis libraries didn't really result in anything useful for me.
But someone gave me a link to the Spoon project to analyze Java code.
Right now, the only parts that I need from Spoon are to give a set of .java files and it will return me the source code in a big data structure provided by Spoon.
So far the code that I use:
private CtPackage analyzeSourceCode()
{
Launcher spoon = new Launcher();
this.javaFiles.forEach(spoon::addInputResource);
spoon.buildModel();
return spoon.getFactory().Package().getRootPackage();
}
After which, I build up whatever I need from the packages returned.
(Note that I also require the actual executions, not only the data structures and members of the source code.)
But this only works for .java files and not for .groovy files... which brings me back to the point where I need a library to analyze Groovy code.
This one also doesn't allow me to use Java and Groovy together, so I need something else anyway.
Can anyone tell me what I am doing wrong in my google searches or provide me with links to a library that can analyze Java and Groovy code?

locate imports definition in java

I'm a newbie in java. Is there any way to find the required imports(or missing imports) in java programming?. I know that in modern editor like Eclipse and Netbeans. But we have to do java in notepad for examination, that's why.
For example, When I use Jframe, I want to know which is the required imports. Is it possible to locate 'import's definition in a directory or file?
Run the compiler javac which tells you the types that cannot be resolved (this imports are missing).
Then you can use the javadoc from the API. In the left lower corner there is a complete list of classes in the API. Search the class and click on it then you know in which package it is.

Tools for finding all dependencies in Java jars

Are there any tools that can be given a classpath or folder, and will search through all the .jar files looking for references to a particular class?
It would have saved me a day of searching in order to resolve this kind of problem.
In my case I had to find out where a rogue reference to a class was coming from, as it was stopping JBoss from starting correctly due to a NoClassDefFoundError. I suppose I am looking for this:
java -jar magicbullet.jar /path/to/search com.myproject.DodgyClass
Searching...
com.problematic.Otherclass in /path/to/search/dodgy.jar implements com.myproject.DodgyClass
Or similar. If not, I'll write one.
Use Tattletale - http://www.jboss.org/tattletale. It helps you create an index which is very handy to search for classes etc.
Under Linux (or Cygwin on Windows) you may use script from https://gist.github.com/980697.
Usage: findclass.sh <directory> <className>
Example: findclass.sh . ClassName (means "search for class named ClassName inside all jars/wars/ears/sars located within current directory and all its subdirectories")
Google for "JarAnalyzer" a helpfull tool doing more exactly what you want (and a bit more ;D )

Setting up test inputs in eclipse

I have some methods that would require to execute over a java class.
For example my method receives as argument a class file, something like:
Information info = grabInformation("class_to_execute");
This method would run the "class_to_execute"and capture its output. And I would like to later assert its output with a given expected value.
My question is: how could I set up eclipse so that my test cases would find the classes that it will execute? Is adding the classes to the build path enough? Are there some variables I could set?
I don't think the CLASSPATH has anything to do with it.
If "class_to_execute" is in another project or JAR, then add it to your Build Path under Libraries. Do you have any reason to believe that's not enough? Build path == CLASSPATH for most purposes.
If you're having Build Path or CLASSPATH problems, it might be easier to debug if you do this:
Information info = grabInformation(class_to_execute.class);
If it can't find the class, then put your cursor on the error and type Control+1. Eclipse might be able to help you fix the Build Path automatically.

Categories