Use a module as library IntelliJ - java

Good evening, I want to use a module as a library on my project. For that I followed some instructions given on some similar questions but with no effect
I'll try to describe my problem as clear as possible.
I have my main project named oncofinder with 2 modules created: oncofinder-gui and oncofinder-core. Let's say I want to import the core model on the gui, like this:
import oncofinder.lib.common;
And I want to declare it like this:
package oncofinder.lib.common;
On the SimpleThreadFactory java class. For that, I would like that the path to be:
oncofinder/oncofinder-core/src/main/java/oncofinder/lib/common/SimpleThreadFactory.java
I already tried to create the library and add it as a dependency as you can see in the follow images:
However, I still get the error:
Package name 'oncofinder.lib.common' does not correspond to the file path '
Any idea what I'm doing wrong and if there is an easier way to import my Modules as dependecies?
Kind Regards

Related

VS Code - The import "#####" cannot be resolved

So, i am running a java project which have many library that are available in the current working directory but VS code seems to not recognize these library and giving out error "The import ###### cannot be resolved" ex: The import org.apache.pdfbox.pdmodel.PDDocument cannot be resolved"
here is the image that might help you to know more about it
This is the package that i am working on :
Here the org/apache is the library contain the class file that are need to be imported and FileArrangement.java is the file having the import statements
Error i have been receiving
this is what VS code is been showing
i really need your help because i really don't have any idea how to correct this
I have checked other projects and they are also showing the same result although the import statements for java classes like . java.util.ArrayList doesn't show any kind of error and i have tried to clean java in VS code it also didn't work
i just need to correct this error of VS code to import the classes that i need
No error on java.util package
Putting the libraries in your current working directory does not work for Java, you need to add them to the classpath.
If you're using maven, that manages the classpath for you.
If not, you can manage it in VS Code by executing the Java: Configure Classpath command from the Command Palette (Ctrl+Shift+P).
You can add dependencies via Referenced libraries under the JAVA PROJECTS panel.
Or use java.project.referencedLibraries setting in settings.json.
For example:
"java.project.referencedLibraries": [
"library/**/*.jar",
"/home/username/lib/foo.jar"
]
Details can be found in configure-classpath and manage-dependencies.

Jpype import cannot find module in jar

I have received the task, at work, to find a way to use some methods from an existent jar file in a Python project. I have very limited experience in Python, but I have worked with that specific jar file before (it is part of a project we are working on). I am not allowed to modify much of both projects, and they are required to be as independent as possible.
I have researched multiple ways to include the jar methods in Python. So far, I have tried Jython (which I cannot use because the Python project uses PyQt among other libraries, which force the use of CPython, if my understanding is correct), Pyjnius and JPype. JPype seems the most promising, but I cannot get it working either. I have pasted the my code below, slightly censored because I don't know how much I am allowed to share.
from jpype import *
import jpype.imports
try:
jpype.addClassPath("jars/sdk.jar") #the relative path to the jar file
jpype.startJVM(convertStrings=False)
java.lang.System.out.println(jpype.getClassPath()) #printing the classpath to check, the path is correctly pointing to the sdk.jar file
java.lang.System.out.println("Hello world") #just to check if jpype is installed correctly, works
jpype.imports.registerDomain("a")
from a.b.c.d.e.f.g.h import SomeClass #fails here
except OSError as err:
print(err) # ToDo: Remove print when done
pass
The error I am getting is that the module a.b.c.d.e.f.g.h.SomeClass could not be found. I have tried different ways to give the path (absolute path, relative path, place the jar in different places in the project and outside of it), but that doesn't seem to be the problem, as the path printed is the correct absolute path to the jar file.
Also, the jar is used in other (Java) projects and it works. It is created using maven package (using IntelliJ, if it is relevant, and the same Java version as the one used by the JPype JVM). In the Java projects, the import would be:
import a.b.c.d.e.f.g.h.SomeClass;
I have copied this and just transformed the syntax into Python.
I have also tried to create the class with JObject (which I probably didn't do right anyway) and also tried the older syntax (to my understanding) with JPackage. For the JPackage way, I am getting the exception that the package a.b.c.d.e.f.g.h.SomeClass.someMethod is not Callable, which to my understanding is an equivalent exception to the one I'm getting using jpype imports. I have already gone through all the questions I could find here with similar problems, but none of those solutions have helped me.
Can anyone suggest some possible solution? Or can anyone see what I'm doing wrong? Suggestions of other possibilities to replace JPype are also welcomed. If there is any clarification needed, I will edit the question.
The only thing that seems likely if the jar is on the classpath and failed to import would be for there to be some missing dependency. You have two other ways to try loading the class which may provide additional diagnostics.
jpype.JClass("a.b.c.d.e.f.g.h.SomeClass")
and
jpype.JClass("java.lang.Class").forName("a.b.c.d.e.f.g.h.SomeClass")
The first is manually loading a class by full class specification. It is mechanically what is happening under the import. The second is calling for Java to load the class (bypassing all of JPype). It returns a java.lang.Class which can be passed to JClass to make a wrapper.
Common failures include missing a jar or native library, attempting to start JPype from within a module and having the wrong relative path, error in initialization of the class due to missing resource. JPype is just calling JNI calls, so if everything is fine on Java end it should work. Given that you checked the java.class.path System variable, it has to be something to do with class resources.
The JPype user manual has an alternatives section if you would like to try to find another package. Most of the alternatives with the exception of PyJnius appear to be unmaintained.

How can I create and Import Libraries in Netbeans - A Full Documented Q

It's me again. So here's the deal, I still don’t really grasp if I am doing something wrong.
I am trying to create my own Personal Library, in which I have certain string methods and so on.
I’ve started by creating a new Project as a Java library:
Then I've added a package to that library:
After that I've created a class in said package:
Here the code of my class:
And cleaned and built that Class//Library. Run -> Clean and Build
And afterwards created the JavaDoc Run -> Generate JavaDoc(MyTestLibrary)
After doing this i decided to add my newly created Library to the Libraries under Tools - > Libraries:
After that I’ve added a new Library (bottom right) and named it TestLibrary:
SO decided to add the .jar File first. I clicked on the “Add JAR/Folder... ” Option and searched for my Projekt Folder (from MyTestLibrary) and went in to the “dist” folder and selected the .jar file:
After that I’ve added the “src”-Folder in “Sources” and the “javadoc”-Folder in “Javadoc”.
After i was finished it was time to create a new Project, to be precise a new Java Application, and try to import my freshly created library. I rightclicked on Libraries and clicked on “Add Library...”:
And imported my Test Library and adding it:
Happy that I've added my Library to my application I decided to import it
import TestPackage.TestClass;.
If I try
public static void main(String[] args)
{
TestClass.
}
The only suggestion is TestClass.class and not TestClass.TestMethod.
Why can't I access the methods of the “TestClass” directly?
How can I see them?
I've tried being as specific as possible. I hope this time I can fully understand what is going on and why I can't access them.
The solution was fairly simple and obvious.
To access the methods without the need of creating an instance of the class i just had to declare them static.

Running Java class with JMeter (Bean Shell)

I have written a Java Class for use in JMeter, packaged the project as a .jar file and moved that file into the lib/ext folder in the jmeter directory. I have seen documentation on how to proceed but they give contradictory answers.
The first way is to use the BeanShell Sampler to import my package and class, create an object of the class and run the methods that way. I have used this method using example classes with more simple file structures than that of class I want to run. The example classes work with the following BeanShell script.
import tools.JmeterTools;
JmeterTools jt = new JmeterTools();
jt.foo();
When I try to use this method for the class I want to run, it states that the variable declaration is an error and the Class cannot be found. I assume this is because I do not understand what to import exactly, as the file structure in my project is a little odd.
The second uses the BeanShell PreProcessor to add the jar to the class path. This method I have not been able to get to work at all, but have read many accounts of others finding success. It works as follows:
addClassPath("directory path to jar\lib\ext\foo.jar");
JMeterTest jtm = new JMeterTest();
jmt.test();
Would anyone have any knowledge of which way would work better or any ideas on how to fix the import?
The import I have been using in the BeanShell script is the following:
import client.JMeterTest;
The package line at the top of my class is the following
import com.x.foo.client;
You need to have your jar file in JMETER_HOME/lib folder.
lib/ext is for JMeter extensions/plugins etc.
Once you have placed your jar, you might have to restart JMeter.
Running external classes from Beanshell should work fine given the following preconditions met
Your test with dependencies is located in JMeter classpath.
JMeter restart is required to pick new libraries up
You need to provide full package name plus full class name (or wildcard) for import.
Either
import com.x.foo.client.JMeterTest;
or
import com.x.foo.client.*;
And finally it is recommended to use JSR223 Sampler and use "groovy" as a language. Beanshell interpreter has severe performance issues so use it for something very "light" like variable amendment, converting variable to property, etc. For generating the real load use JSR223 and groovy as it implements Compilable interface and hence you can achieve performance similar to native Java code. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for detailed explanation, benchmarking and instructions on installation of groovy scripting engine support.
For anyone who has this issue in the future. The answers given by others are correct. It wasn't working for me because I had forgotten that Maven does not package files in the test directory when a jar is made.
This link may help if anyone ever does this in the future.
Generate test-jar along with jar file in test package

python import as equivalent in Java?

I have a bunch of idl files that automatically create four packages, with a lot of java files into it.
I need to insert those java files in a com.bla. package architecture.
Thing is in my generated files I have imports UCO.State for example, that do not fit with my new package architecture.
So question is : Is there a java equivalent to 'import com.bla as bla' ?
The only other option I see is to import the UCO package and rename all UCO.State and other directly by State.
But that would mean refactoring hundreds of files o_O.
Any idea ?
Thanks !
Import all the files in Eclipse. If you manage to get the code compile using the refactor functions of the IDE it will save you all the trouble.
There is no functionality of adding synonyms to the imports in java, but even if there was such how would that have helped you? You still will need to change all your files.
I found the solution, lying in the documentation of my idlj generation tool.
http://docs.oracle.com/javase/1.4.2/docs/guide/rmi-iiop/toJavaPortableUG.html
You should search for information in the pkgTranslate and pkgPrefix options :)
This way, the tool automatically changes foo => com.bla.foo
Problem solved !

Categories

Resources