I am trying to get Maven to compile Scala 3, but I get weird stuff like
[INFO] --- scala-maven-plugin:4.5.4:compile (default) # laboratory ---
[INFO] Using incremental compilation using Mixed compile order
[INFO] compiling 1 Scala source and 10 Java sources to C:\Users\ERIC\Documents\git\loom-lab\laboratory\target\classes ...
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\Scala\net\kolotyluk\loom\HelloScala.scala:<233..233>: '=' expected, but '{' found
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Context.java:<332..332>: unclosed string literal
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Context.java:<595..595>: unclosed string literal
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Context.java:<666..744>: '}' expected but eof found.
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Experiment04_PrimeThreads.java:<16902..16902>: unclosed string literal
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Experiment04_PrimeThreads.java:<17176..17176>: unclosed string literal
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Experiment04_PrimeThreads.java:<28863..28870>: '}' expected but eof found.
[ERROR] 7 errors found
First of all, why are the .java files failing too?
The code for HelloScala.scala is
package net.kolotyluk.loom
import java.time.Instant
import java.util.concurrent.StructuredExecutor
import scala.util.Using
object HelloScala {
def main(args: Array[String]) {
Context.printHeader(HelloScala.getClass)
val results =
Using(StructuredExecutor.open("HelloScala")) { structuredExecutor =>
val futureResults = (0 to 15).map { item =>
println(s"item = $item, Thread ID = ${Thread.currentThread}")
structuredExecutor.fork { () =>
println(s"\ttask = $item, Thread ID = ${Thread.currentThread}")
item
}
}
futureResults.map(_.get)
}
println(results)
}
}
And this compiles just fine using Scala 2.13, so the error message does not make sense.
As an aside, in IntelliJ, I cannot get Scala 3 to build, that fails for different reasons...
The reason for the error in HelloScala.scala is that procedure syntax was dropped: https://docs.scala-lang.org/scala3/reference/dropped-features/procedure-syntax.html
Instead, use
def main(args: Array[String]): Unit = {
Related
I would have a Scala question. I have the following code while using resilience4j library. The code works like a charm in Scala 12, but my project, a client library uses Scala-Cross compiling to Scala 11 also. When building it under Scala 11 I get the following error. Anyone has an idea how this should be written in Scala 11 to also compile?
import io.github.resilience4j.retry.Retry
import java.util.function.{Supplier, Function => JavaFunction}
val supplier: Supplier[List[Endpoint]] = () => getEndpoints
val decoratedSupplier = Retry.decorateSupplier(retry, supplier)
val result = io.vavr.control.Try.ofSupplier(decoratedSupplier)
.onSuccess { endpoints => endpoints }
.onFailure { failure =>
// handle failure
}.get()
In Scala 11 I get the following errors:
[Error] ServiceDiscoveryClient.scala:38: type mismatch;
found : () => List[Endpoint]
required: java.util.function.Supplier[List[Endpoint]]
[Error] ServiceDiscoveryClient.scala:41: missing parameter type
[Error] ServiceDiscoveryClient.scala:97: type mismatch;
found : () => List[Endpoint]
required: java.util.function.Supplier[List[Endpoint]]
four errors found
Any ideas? Thanks!
In Scala 2.11 you need -Xexperimental compiler option to support passing lambdas as SAM types such as java.util.function.Supplier. In 2.12 it's enabled by default. You can either add this option, or add scala-java8-compat dependency and
import scala.compat.java8.FunctionConverters._
...
val supplier: Supplier[List[Endpoint]] = (() => getEndpoints).asJava
(asJava may also be needed for lambdas used in result).
I am currently migrating a project to JDK 11 and compiling it with Maven. However, Maven throws a wobbly over a single method reference (it doesn't have issues elsewhere). The method in question looks like this:
public class MyThing {
boolean something = true;
// ...
public boolean isSomething() {
return something;
}
// ...
}
And the call to the above method looks more or less like this:
return methodThatGetsOptionalDefinition(aString) // Optional<Definition>
.map(definition -> defenition.getMyThing(anotherString)) // Optional<MyThing>
.map(MyThing::isSomething) // Optional<Boolean>
.orElse(true);
Compiling this, Maven throws the following message:
> mvn clean install -Pdist-snapshot -DskipTests
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project XXXXX: Compilation failure
[ERROR] /D:/XXXXX.java:[63,38] incompatible types: invalid method reference
[ERROR] method isSomething in class XXXXX.MyThing cannot be applied to given types
[ERROR] required: no arguments
[ERROR] found: java.lang.Object
[ERROR] reason: actual and formal argument lists differ in length
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
If I expand the method reference like so, it compiles without a problem:
return methodThatGetsOptionalDefinition(aString)
.map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
Typing the Optional also works:
return methodThatGetsOptionalDefinition(aString)
.<MyThing>map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
The same error occurs when compiling the project using IntelliJ with both Open JDK versions 11.0.1 and 11.0.2. Ironically, IntelliJ complains that the "Lambda can be replaced with method reference". The problem also occurs when using a different "language mode".
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
Does anyone have any idea why this might be happening?
Some of the specific implementation details have been obfuscated.
It has to do with the Type definition of the map method.
In order to provide a method reference, the method must have a matching Type definition. Stream map has a function method, but yours is a producer function.
Function = (T) -> R
Producer = () -> R
So, Method::isSomething() is not compatible with Stream.map, but your inline closure myThing -> myThing.isSomething() is..
After upgrade of JBoss Fuse 6.1 -> 6.2 and as a result Apache Camel 2.12 -> 2.15, the following compilation error occurred in one of our custom components:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] [redacted].java:[11] error: annotation UriEndpoint is missing values for attributes title,syntax
[ERROR] [redacted].java:[8] error: annotation UriEndpoint is missing values for attributes title,syntax
[INFO] 2 errors
*-note that the [redacted] means "removed" or "anonymized" part
The solution was to change from:
#UriEndpoint(scheme = "[redacted]")
to:
#UriEndpoint(scheme = "[redacted]", syntax = "scheme:host:port/path", title = "[redacted]")
More information in #UriEndpoint JavaDoc
The documentation is quite enigmatic ATM.
In my current project, i have this code:
for(Annotation annotation : field.getAnnotations()) {
String package = annotation.annotationType().getPackage().getName();
if(package.equals("com.loja.annotations.input_type"))
input.setAttribute("type", annotation.annotationType().getSimpleName());
}
when i try build the project, this code causes a compilation failure due to this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project store: Compilation failure: Compilation failure:
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[37,11] not a statement
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[37,17] ';' expected
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,14] illegal start of expression
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,21] illegal start of expression
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,28] ';' expected
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,22] variable declaration not allowed here
anyone can see what's wrong here?
line 37 is: String package = annotation.annotationType().getPackage().getName(); and line 38 is if(package.equals("com.loja.annotations.input_type"))
you can try this..
annotation.annotationType().getName();
You can print out your "package" variable after line 37 (before if).
I wrote and ran the similar code and it worked for me. I created a custom annotation in my api package and line 37 returned "api".
I have added the simple code write to a file into an open source project (Saiku) that before my changes builds and compiles cleanly.
The program compiles cleanly in Centos 5.3 Eclipse (no red X's).
However, when rerunning the maven build script, the compiler errors are generated (Exhibit 1):
Does Maven compile java projects differently from Eclipse?
Exhibit 1:
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[67,8] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[67,11] ';' expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[70,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[70,15] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[71,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[73,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[75,11] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[79,2] invalid method declaration; return type required
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[130,15] class, interface, or enum expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[130,43] class, interface, or enum expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[131,8] class, interface, or enum expected
[ERROR]
Exhibit 2:
import java.io.*; to the import section
try {
FileWriter fstream = new FileWriter("/usr/local/dailycandy/biserver.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.flush();
//Close the output stream
out.close();
}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
The code that you have added is not within a method/constructor/block etc. This is not valid Java, and thus does not compile.
You need to surround this with something like follows:
public void doSomething
{
// Insert code here
}
Alternatively, you can place it in an existing method, or constructor, depending on when you need this code to execute.
Why Eclipse is not highlighting this error is beyond me. It would normally report this. Try refreshing your project or cleaning/rebuilding and you should see that it will fail to compile.