SBT project with a Java module in IntelliJ - java

I am learning developing project in IntelliJ.
I started a new SBT project. Now I have two files, one is a Scala script projectName/src/main/scala/scalaScript.scala and the other is a Java class projectName/src/main/java/moduleName/MyClass.java
The Scala script is used for me to test codes line-by-line in a Scala Console. In oder to test the class I defined in MyClass.java, I think I need to compile it first and then import moduleName.MyClass in the Scala script. After that, I can use the java class to create objects and do whatever I want.
If I am not using IntelliJ, I just need to go to the java module directory and call javac -cf myJavaLibrary.jar MyClass.java to create a jar. Then move myJavaLibrary.jar into the same directory as that of my Scala script file.
How do I efficiently do that in IntelliJ? What is the working pipeline?
Update 1
I switched to using Scala worksheet. However, after import moduleName.MyClass, there is an error saying the module is not found.
import bestbuy.Laptop
where bestbuy is the module name, and Laptop is a class. Then the message says:
Error:not found: value bestbuy
import bestbuy.Laptop
^
Error:(6, 10) not found: value bestbuy
;import bestbuy.Laptop
^
Error:(18, 50) not found: value bestbuy
println(MacroPrinter211.printImportInfo({import bestbuy.Laptop;}))
^
Error:(26, 50) not found: value bestbuy
println(MacroPrinter211.printImportInfo({import bestbuy.Laptop;}))
^
Update 2
In the worksheet, you should check the little box in front of "Make project" so that all the source code will be compiled and the byte code will be saved in the target directory where SBT expects. Then I can successfully import bestbuy.Laptop in the worksheet.
However, I am still expecting an answer to my original question. If I can import bestbuy.Laptop in the Scala script file instead of a worksheet. In the worksheet all codes are executed at once, while I would like to execute them one line at a time.
After the project is compiled, I tried executing import bestbuy.Laptop in the Scala Console, but got error:
scala> import bestbuy.Laptop
<console>:13: error: not found: value bestbuy
import bestbuy.Laptop
^
It seems that the console's class path is not properly configured. How can I configure the console's class path?

to use repl-like environment in IntelliJ just create new scala worksheet file and in there you can import your Java class and use it. IDE will compile everything for you.
You can also use terminal, type in sbt console and you will have your repl with your project classes in classpath, so you can import and use them.

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 do I set up extJWNL in a Java project?

I am trying to determine whether a set of strings, from an English sentence, are all words from the WordNet dictionary. I put the JARs in a folder in my project in eclipse.
I've downloaded the binary release of extJWNL and put the JARs in a folder called lib.
I also added these jar files to the class path and the module path using Right Click > Build Path > Configure Build Path:
I used the following code to try to import dictionary
import net.sf.extjwnl.dictionary;
This error is shown on the import statement
The package net.sf.extjwnl.dictionary is accessible from more than one module:
<unnamed>, extjwnl
I thought that this error was showing up because it was a package, not a class/type. But adding a new class and trying to change the package doesn't show this new package, namely WORDNET_JARS, just the default package.
Why is this error being returned and what do I need to do to get rid of the error and import the wordnet packages?
Platforms
I am using Eclipse IDE, and write all this code in Java. The API I am trying to import is the WordNet API.
Edit (8/21/2019)
By removing the module path, it gives a new error:
Only a type can be imported. net.sf.extjwnl.dictionary resolves to a package
I'm surprised it was this easy:
Instead of
import net.sf.extjwnl.dictionary
I had to do
import net.sf.extjwnl.dictionary.*
to get all the types. The only other thing that had to be done had already been done where I add the JARs to the class path

how to import joda time in java on Ubuntu system

I'd like to generate dates sequence in a range. This thread suggests to use Joda-Time package. I downloaded it and unzipped it to the same directory as my Main.java.
When I try import ./joda-time-2.7/org.joda.time.DateTime;, the compiler says:
Main.java:4: error: expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
Main.java:4: error: expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
Main.java:4: error: class, interface, or enum expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
And when I try import org.joda.time.DateTime;, the compiler says:
MissingDateSearch.java:5: error: package org.joda.time does not exist
import org.joda.time.DateTime;
^
It seems that I didn't include the package into my building path. This thread discusses how to set the building path for java. Currently, my "environment" file only has PATH variable. I don't want to make any global change just for one project.
So my question is, does any one know how to include the package in a way without global change? Or does any one has a simple way to generate dates in a range without joda-time?
Thanks!
ADDED
This problem can be bypassed by using IDEs like eclipse. Any terminal based solutions are still welcomed.
Use the
import org.joda.time.DateTime;
import statement and ensure that JodaTime jar file is on your classpath at compile & runtime

setting the correct classpath for compiling and running Java packages? [duplicate]

This question already has an answer here:
getting error when I compile the Java code using package in commandline?
(1 answer)
Closed 8 years ago.
I have been using Eclipse lately, where compiling and running the program is very simple. Not much needs to be done in setting the classpath. But apparently that is not the case when it comes to running them from commandLine. when I try compiling from terminal, I am having various errors. I am pasting an image of my package structure of the project cp125_soln. Now I want to compile Invoice.Java in the com.scg.domain package,
I tried
javac src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/domain/Invoice.java:17: error: package com.scg.util does not exist
import com.scg.util.StateCode;
.......................//long error message
This means I do not have com.scg.util.* in my classpath. so I tried
javac -cp src/main/java/com/scg/util/* src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/util/ListFactory.java:8: error: package org.slf4j does not exist
import org.slf4j.Logger;
^
src/main/java/com/scg/util/ListFactory.java:9: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
^
src/main/java/com/scg/util/ListFactory.java:11: error: cannot find symbol
import com.scg.domain.ClientAccount;
^
symbol: class ClientAccount
location: package com.scg.domain
................... // long error message
I read different articles on how classpath works and how to provide it in command-line. but when it comes topackage level structures, I am not able to find a good tutorial on how to COMPILE and RUN packages. If a little help could be provided here on the propery way to compile and run these kind of packages, it will be very helpful.
javac src/main/java/com/scg/domain/Invoice.java
Try this:
cd src/main/java
javac com/scg/domain/Invoice.java

Java importing packages

I have a program in my desktop that I want to run (structure in the url), but when I do compile, with
\code\nlp\assignments\parsing\javac PCFGParserTester.java
I get:
PCFGParserTester.java:6: error: package nlp.io does not exist
import nlp.io.PennTreebankReader;
^
PCFGParserTester.java:7: error: package nlp.ling does not exist
import nlp.ling.Tree;
^
PCFGParserTester.java:8: error: package nlp.ling does not exist
import nlp.ling.Trees;
^
PCFGParserTester.java:9: error: package nlp.parser does not exist
import nlp.parser.EnglishPennTreebankParseEvaluator;
how do I get my program to correctly import my packages?
You want to be in the \code directory and compile with:
javac nlp\assignments\parsing\PCFGParserTest.java
(And you should have a package declaration of package nlp.assignments.parsing; to match the position in the directory structure.)
That way javac will look for the other classes appropriately.
Alternatively, and rather more simply, you could use an IDE such as Eclipse or NetBeans, and it would take care of all this for you - you'd just specify the code directory as a source directory, and all would be well.

Categories