Where org.reflections takes package from if i pass empty? - java

i am having a problem when using reflections. I have this code in a project, which is implemented as maven dependency by other projects to find testNG test classes. So, as i haven't got a particular package, i was passing it empty and it was finding successfully classes i want when i execute locally. Here is my code:
String pack = StringUtils.EMPTY;
this.reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(pack)).setScanners(
new MethodAnnotationsScanner()));
this.testNGTests = this.reflections.getMethodsAnnotatedWith(org.testng.annotations.Test.class);
But i have the problem that when i execute it remotely it can't find any classes. So i was thinking that i must pass it something at package parameter. So, i want to know if any of you have and idea about that happens behind reflections when i pass empty package and why is working. Do you know something that could help me? Thank you!

I've already fixed my problem. I changed the setting for package by forJavaClassPath. Thanks you! Yet, if any of you could give me some more information it will be well recieved! Regards

Related

In cannot be resolved to a type

I am following Prinston's course Algorithms, Part I. As I came from .NET and just started to use Java, I have some issue with a piece of Java code and I can not find any related information. They provide with a code which I can use for reading a text file:
public static void main(String[] args) {
In in = new In(args[0]);
int n = in.readInt();
...
}
Exception thrown:
In cannot be resolved to a type
What is this In? Should I import some package or what should I do?
The whole code and description can be also seen here: http://coursera.cs.princeton.edu/algs4/assignments/collinear.html
You need algs4, provided by Princeton as well. When it's in the classpath, add
import edu.princeton.cs.algs4.In;
If that's the only class, you could use the source of In.java.
But I doubt that's allowed on Coursera: when you submit code, it will probably be compiled on the server with algs4.jar in the classpath, so you should really use that one and not your own code.
In.java seems to be a class that someone at princeton made, probably for that same course: http://algs4.cs.princeton.edu/12oop/In.java.
So, yes, you need to import the correct package and you need a jar with that class in your classpath. Search your course documentation for more hints.

cannot resolved to a type error in java

I'm new in java, please help me to understand this.
I can see there is ReadHtml class and defined with one public method. But when i put this code in ecplise, it shows red mark under WebClient with tag that "this cannot resolved to a type". May I know what does it mean? Gone through all about method definition but couldn't find any remedy to understand this.
Can I get any help ?
public class ReadHtml {
public static LinkedList<String> readJacksonCounty(String urlName, String pStartDate,String pFinishDate)
{
LinkedList<String> xmlListReturn=new LinkedList<String>();
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");
final WebClient webClient1 = new WebClient(BrowserVersion.CHROME);
webClient1.setJavaScriptTimeout(60000);
webClient1.getCookieManager().setCookiesEnabled(true);//enable cookies
webClient1.getCache().clear();
You are missing an import of this library:
import com.gargoylesoftware.htmlunit.WebClient;
Add this to the top of your file (and read dsp_user's comment for future reference).
Basically "...cannot be resolved to a type" means that type is not available on the class path. If you're just using eclipse refere to How to import a jar in Eclipse.
If you already added the needed jar onto your class path, you are missing the import statement. Imports just make it so that you dont have to use a class's fully qualified name. (you can type
MyClass myClass;
as opposed to
com.some.package.MyClass myClass;
if you add
import com.some.package.MyClass;
at the top of your file.
Note that if you want to build a jar from your project you'll need some kind of build tool. If you choose to use Maven, which is very common, just read any tutorial on how to get started and manage dependencies.

Importing Libraries Java

So I have two Jars I want to use in my project from here http://www.jhlabs.com/ip/filters/index.html
I added both CheckFilter and MarbleFilter to my class path. But when I do
CheckFilter();
It says I have to create a method CheckFilter()
I'm pretty sure that's the method I need to call to use that effect. But when I try any of the other methods in the library Jar it still gives me the same thing.
I have no experience with importing/using external libraries. Any help would be great.
checkFilter = new CheckFilter();
CheckFilter();
I tried above and it says I need to create a local variable checkFilter
How are you writing up the code. I will suggest to use eclipse IDE, it will make your tasks simple
If you are using eclipse. You need to do import the jar Filters.jar to your build path
which as you mentioned you downloaded from JHLabs Download page
I found Filters.jar inside dist directory.
Then you will be able to import the class or package
import com.jhlabs.image.*;
OR
import com.jhlabs.image.CheckFilter;
After importing the class or package you will be able to create object to it by
CheckFilter checkFilter = new CheckFilter();
In case you are totally new you can take help from people over IRC or chat and get going.
Someone would be able to quickly help you out
----==----==----==----==----==----==----==----==----
Read your comments and Question again.
You are totally missing the point. If you call to CheckFilter() directly without invoking new keyword, compiler will consider you are trying to access a method which is inside the class you are writing up. and give you error.
As I mentioned above. Your are trying to accessing Instance variable for the class without declaring it. Either do
CheckFilter checkFilter;
before you access checkFilter variable or directly instantiate the class the way I mentioned.
Seems to me you are missing a log of points :D
Methods don't exist without a class. That is probably the constructor to a class. Use
CheckFilter checkFilter = new CheckFilter();
instead. Then call methods on checkFilter.
From #andy-thomas in this similar question
The import statement imports classes from the jar file, not the jar file itself.
An import statement of the form:
import stdlib.*;
will import all the classes in the package stdlib.
Oracle provides this tutorial on importing.
This statement
CheckFilter();
Tries to call a method CheckFilter defined in your class, which is not the case. If this is a utility function, this may be a static method, in which case you can call it like this
ClassName.CheckFilter(); // replace ClassName with the class containing this function
If not, then you may have to instantiate an object
ClassName obj = new ClassName();
obj.CheckFilter();
or skip the object variable
new ClassName().CheckFilter(); // Not prefered
To add jar files, right click on your project in "Package Explorer", go to "Configure Build Path" and then to "Add External Jars".
Set the jar (libraries) into your classpath and use import statements in your java code to include the required Classes.

Having problems while trying to execute an Acceleo module in standalone mode

I have successfully created an Acceleo module for M2T purposes and am trying to execute it from a Java program.
This is what I tried :
String[] str = {"/home/hamza/workspace/HLRedundancy/model/System1.xmi", "/home/hamza/workspace/HLRedundancy/"};
Generate.main(str);
Generate being the name of the Acceleo module I created and thus, the name of the Java class containing the Acceleo generation methods.
Here is the error I'm always getting :
Exception in thread "main" org.eclipse.acceleo.engine.AcceleoEvaluationException: The type of the first parameter of the main template named 'generateElement' is a proxy.
at org.eclipse.acceleo.engine.service.AcceleoService.doGenerate(AcceleoService.java:566)
at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.generate(AbstractAcceleoGenerator.java:193)
at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.doGenerate(AbstractAcceleoGenerator.java:158)
at HighLevelGenerator.main.Generate.doGenerate(Generate.java:250)
at HighLevelGenerator.main.Generate.main(Generate.java:160)
at Execute.main(Execute.java:11)
I've been searching for a while about this error but I have no idea about its cause.
Any idea about a solution to my problem ?
Thanks
The most common cause of this issue is failure in properly registering the metamodel and factory corresponding to your inpu model (System1.xmi).
If you look at the comments in the generated class "Generate.java", you will notice a number of places where we indicate steps to follow if running in standalone. The most important begin registerPackages where you are required to register your metamodel.
If you debug the launch down to the point where the model is loaded (place a breakpoint right after the line model = ModelUtils.load(newModelURI, modelResourceSet);), you can look at the model.eResource().getErrors() list to see whether there were errors loading your model.
You might also be interested in looking at this video describing the process (registration required) .
Check out the first line of your acceleo module,
what is the URI of the metamodel? Does it start with 'http://' ?
Maybe this can help:
Acceleo stand alone - first parameter is proxy
This issue happen when your meta model contains sub-packages and the top package not contain any class.
to solve the problem, add a Dummy class the the top package and regenerate the meta-model code. It worked fine for me.

JUnit #Test InvalidStateException

I'm having a problem with my test application which is follows. I'm having following code:
#Test(expected = InvalidStateException.class)
public void testLiianPitkaKayttajatunnus()
{...}
I'm using Eclipse and it says "InvalidStateException cannot be resolved to a type" and as we know if eclipse notice a problem it underline it with red wave line. I have been trying to search solutions but still not found any. I have tried following things but none of them has solved the problem:
Adding org.apache.openjpa dependency to my pom.xml and adding line before class code "import org.apache.openjpa.persistence.InvalidStateException;"
In thread, it is said that if you have in your class extension of other class, it should be taken out. This didn't helped neither.
In the same thread one advice is to use JUnit 4.4 and it didn't helped neither.
So my question is how this problem can be solved?
For additional information, my class start follows (imports are not mentioned here)
public class TietorakenneTest extends TietokantaTestCase {....} and JUnit version that I use is 4.10,
any help is appreciated. If you need any other information about the code, please let me know and I will put them here.
Qualify the class by adding the package name.
#Test(expected = org.apache.openjpa.persistence.InvalidStateException.class)
Change your import statement to
org.apache.openjpa.util.InvalidStateException and as you mentioned add org.apache.openjpa dependency to my pom.xml
Hope it helps.

Categories

Resources