maven


Error trying to instantiate a Kotlin class from Java


I am trying to instantiate a Kotlin class from Java but every time I try to compile with Maven I get the error cannot find symbol:
class ConfigCommand(private val game: Game) : Command("config", "") {
init {
addAliases("cfg")
}
override fun getRequiredRank(): Rank? {
return null
}
override fun getDescription(): String {
return "Shows the config for the game"
}
#Throws(CommandException::class)
override fun execute(sender: CommandSender, args: Array<String>): Boolean {
if (args.isEmpty()) {
if (sender !is Player)
throw NoConsoleAccessException()
sender.openInventory(ConfigGUI(game).build())
return true
}
return false
}
}
Not sure why that didn't format correctly but anyway before I converted it to a Kotlin class it worked but I need to register this command in my main class which is a Java class. When I try to instantiate a Kotlin class from a Java class there are no errors in the IDE but when I go to compile it maven screams
cannot find symbol
[ERROR] symbol: class ConfigCommand
I'm still figuring out Kotlin, but I tried to work through a few permutations based on your example. I was easily able to create your problem based on your Hastebin pom.
You were right that changing the <phase> to process-sources got my test code to compile in Maven, but I (honestly) can't always remember all the Maven phases off the top of my head so I wouldn't personally be comfortable with changing to process-sources without more research -- especially since the IntelliJ tooling relies on the compile phase.
After playing around with my own interop examples, it seems like the critical (and missing from the tooling defaults) piece is the <sourceDirectory> element as a top-level child of <build> as in:
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
Adding the <sourceDirectory> as a top-level element under <build> made my Java to Kotlin interop code compile when I ran the mvn compile terminal command. That was true when I mixed the source files in the "java" directory to contain both Java and Kotlin files.
As a side note (and I don't I understand why as I write this), when I added "Kotlin" as part of my class name to my Kotlin source I didn't need to add the <sourceDirectory> element...
After going trough this page multiple times and realizing that I had done nothing wrong I started to just mess around with my pom and eventually got it working by changing the phase of the Kotlin compile to process-sources
The maven-compiler-plugin adds default executions default-compile and default-testCompile that run first in the corresponding phases.
In order to use Kotlin classes in Java code, you need to run Kotlin compiler before Java compiler runs. One way to do it is to schedule its execution to process-sources phase. Another approach is to "unschedule" default executions of Java plugin and schedule new executions instead after the execution of Kotlin plugin.
The default executions of Java plugin are turned off with this section of pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
...
</executions>
</plugin>
And then new executions are added:
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
The full example is shown here: https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources

Related Links

Using Maven-JarJar plugin
Mule Server 3.7.0 error: “Failure to find com.mulesoft.weave:mule-plugin-weave_2.11:jar:3.7.0”
How to configure maven multi module project and FindBugs?
Can Karaf download dependencies from maven repository on its own?
How to change maven home directory?
Will fluentlenium pom.xml dependncy download all dependancies that it requires?
Couchbase Template Version 1.4.2
How to deploy a WAR file from s3 to AWS EC2?
Got “Could not find resource 'src/checkstyle/checkstyle-suppressions.xml” when using spirng boot 1.3.1
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ee:object-store-caching-strategy'
problems building a maven project with bower on a windows VM
Tycho build fails due to missing bundle “org.eclipse.equinox.p2.reconciler.dropins” for an e4 RCP application
Maven multi module project import module
Copy library from remote to local repo
Execute multiple goals with parameters in maven
Maven profiles with variable for properties

Categories

HOME
maven
sendgrid
pdf
listview
netbeans
yahoo-oauth
angular-ui-bootstrap
flyway4
networkx
mouse
bs4
virtualization
primary-key
ssl-client-authentication
etl
hex-editors
try-catch
lombok
opentracing
tostring
windows-7-x64
alpine
predix
visjs
java-3d
tokenize
kvc
claims-based-identity
cas
semantic-analysis
newline
librato
contact-form
catel
catch-all
perlin-noise
protovis
textmate
mapbox-gl
restlet
withings
android-browser
ssjs
document.write
webdriver-manager
awt
isbn
turbogears
bootstrap-dialog
babel-core
scrollable
network-flow
theming
logparser
datastax-startup
git-diff
tropo
nodebb
spring-mongodb
bind9
account-kit
vtigercrm
word-vba-mac
infix-notation
radtreelist
multi-level
skobbler-maps
merge-conflict-resolution
master-slave
cloudbees
uid
srand
cartesian-product
lemon
pundit
ios9.1
myo
formatjs
website-monitoring
file-copying
onactivityresult
android-radiobutton
starcluster
gwt-rpc
ocunit
angularjs-controller
gil
broken-links
twrequest
chrono
bubble-chart
eclipse-templates
deobfuscation
xmemcached
paster
sortable-tables
web-application-design
mirah
adrotator
mtj
avatar
anti-piracy
private-members
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App