How does one import a scala package in java? - java

I have a gradle project; I have some scala classes in scala.com.EssencePVP.models that I want to import into my java code, which has the same package name for that matter.
The structure looks something like this:
src/main/java/com/EssencePVP/
src/main/scala/com/EssencePVP/models/
when I try to import my scala package (import com.EssencePVP.models.*) into java, I get this error message:
EssencePVPMod/build/sources/java/com/EssencePVP/EssencePVP.java:56: error: package com.EssencePVP.models does not exist
clearly the problem is that is trying to look in the java directory, and not the scala directory. How do I fix this issue?
Thanks you.

According to Gradle Scala plugin documentation, it seems that it only allows Java code in src/main/scala to be compiled together with Scala code. You could try to set srcDirs = ["src/main/java", "src/main/scala"] for Scala plugin and correspondingly remove src/main/java from Java srcDirs.
Note that Maven and SBT don't have this limitation.

Related

Using Jsoup library on eclipse [duplicate]

I have just recently started using Eclipse and am running into problems trying to install external libraries. Following online tutorials, I add the .jar file to the classpath and I see it in the referenced libraries folder. Despite this, when trying to import, I get the error:
The package org.apache.commons is not accessible
For reference, I am trying to install the apache math commons library.
Your code probably has two issues.
First, the import statement is wrong since in Java you cannot add a package itself, but all classes of a package as follows (note .*; at the end):
import org.apache.commons.math4.linear.*;
or a specific class, e.g.
import org.apache.commons.math4.linear.FieldMatrix;
Second, you use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9 and you have Java 12.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

How to create a Java package from downloaded source in Linux

I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?
I moved the downloaded directory into the same directory as my java class files.
What I've tried so far:
package commons-codec-1.11-src;
I place this at the head of my java class file
Then i try and compile the file using javac in the Linux terminal
javac -cp ~/Documents/javapractice/commons-codec-1.11-src ~/Documents/javapractice/File.java
I get a "interface, class, or enum required error" and the compiler error points to the package statement in the java class file.
code:
import java.util.*
package commons-codec-1.11-src;
public class File
{
........
}
Just to clear things up commons-codec-1.11-src is source code I downloaded and is now a directory in the same directory as File.java
Any help would be greatly appreciated! Thank You!
I downloaded an external library, common-codecs, and am trying to
create a package from the downloaded source code so that i can import
and use it in java class files. How would i go about doing this?
You don't need and you should not package the source code of the external library in your application.
Extracting dependency classes in your own application is a very corner use case and it should done only as you have no choice.
What you need is adding the jar that contains the compiled classes in your classpath at compilation (javac command) and at runtime (java command).
Supposing that the jar is named commons-codec-1.11.jar, to compile your File.java class you should execute :
javac -cp ~/Documents/javapractice/commons-codec-1.11.jar /~/Documents/javapractice/File.java
The File.java declaration is not correct either.
The package declaration has to happen before the import declaration and the package and import values are not correct either.
It should be something as :
package javapractice;
import java.util.*;
public class File {
........
}
About import from the third party library, you need to import classes you use in File class.
You cannot import the whole package as you try to do.
I think that you should try to understand javac/java bases and start with an IDE to make things easier.

Integrating a Java Class into a PyDev Project (Jython) in Eclipse

I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.

Apache Commons Lang Exception

I am trying to use StringUtil class from Apache Commons Lang jar (commons-lang3-3.1-bin.zip).
So I added this jar to my class path and I ran that program.
When I ran my code I am getting an exception like
"Caused by:java.lang.ClassNotFoundException:org.apache.commons.lang.StringUtils".
I opened this class using java decompiler and when I opened its showing as
"// INTERNAL ERROR //". Except this class all other classes are fine.
After that I downloaded the source code and I compile that class and I opened that compiled class in java decompiler then it also shows the same error. So how can I solve this issue and how can I use this issue
Between commons-lang 2.x.x and 3.x.x the packages have move from org.apache.commons.lang (that is missing to your code according to the exception) to org.apache.commons.lang3 as presented in your screenshot.
Either you downgrade to commons-lang 2.6 or you update your code to change the import declaration from org.apache.commons.lang.StringUtils to org.apache.commons.lang3.StringUtils
Make sure the file you downloaded is right.
Make sure the jar you added to your project isn't commons-lang3-3.1-bin.zip, but is commons-lang3-3.1.jar in commons-lang3-3.1-bin.zip.
http://commons.apache.org/proper/commons-lang/article3_0.html
Java code Despite the label of backwards incompatibility, in the vast
majority of cases the simple addition of a '3' to an import statement
will suffice for your migration.
Change: import org.apache.commons.lang -> import
org.apache.commons.lang3

How can I install additional packages in Java?

I am very new to Java.
I would like to use some features from package called daj
The tutorial code has the following lines.
import daj.*;
import java.util.*;
import java.lang.Math;
import Msg;
But first and fourth lines produce red underscore such that program cannot compiles.
How can I resolve this problem?
You have to add the packages to the classpath. If you are using an IDE as Eclipse or Netbeans, go to the project options and find the "Add library/jar" and they will take care of you.
You will need to add these libraries to the classpath both to compile and to run your program.
Also, the usual way of ordering imports is:
first, java. packages
second, javax. packagaes
third party packages
your packages
You need to have them in your build path.
That daj package or jar file should be in your package or current directory or in classpath.

Categories