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

Jenkins maven-project plugin doesn't support reactor module exclusion
Where to actually put internal repository URL?
Error appears while configuring Hue on my ubuntu 12.04
Maven: print out “reactor summary” every time.
Unable to build Maven project
Should I put openjpa dependency in openjpa maven plugin if it is already in dependencies?
Maven3 | Copy file hosted on one server to my code repository and then compile them
Disable the integration test phase in pom.xml
new Vaadin on new Netbeans — the basic dependencies
Can Maven a plugin use itself as a plugin?
Gradle Multiproject jar doesnt include dependent classes
How to exclude transitive dependencies of spring-boot-dependencies from maven import scope
Is there a way to get all the test methods in a maven project
Repository for Camel ssh version 2.11.5?
Maven project as a dependency module of gradle project
Gradle dependency - com.google.auto:auto-common:1.0-SNAPSHOT

Categories

HOME
sendgrid
magnific-popup
c#-4.0
hp-exstream
gis
getelementsbytagname
google-oauth
amortized-analysis
jgroups
ckeditor
opengl-es-2.0
webrequest
visual-studio-cordova
flask-wtforms
iron-router
netflix
fatal-error
oracle-coherence
jtextfield
nas
amazonsellercentral
physics-engine
extjs5
p-value
h2db
facet
entitlements
jaxb2
vision
.net-4.0
exuberant-ctags
winrt-xaml-toolkit
usbserial
hybridauth
repo
xmlreader
android-ble
devextreme
vxworks
retina-display
automake
withings
c11
google-closure
bootstrap-dialog
s
segment
websphere-mq-fte
strptime
komodoedit
winscp-net
django-scheduler
blacklist
messenger
url-pattern
infix-notation
fody
blackberry-10
goose
asp.net5
phpcas
browser-link
ora-00900
qdialog
quicklisp
android-listview
appfabric-cache
sailfish-os
mmc
e10s
apache-commons-net
lustre
modalpopup
jsonpickle
angularjs-ng-click
file-copying
titanium-modules
preferences
php-5.4
srs
centos5
oam
ms-project-server-2010
commoncrypto
dexterity
tidy
jmapviewer
qt-faststart
django-nonrel
spring-validator
pygit2
free-variable
automount
armcc
jspinclude
hosts-file
appconkit
online-compilation
windows-phone-7.1.1
code-cleanup
cufon
qt-jambi
psi
xmemcached
inotifycollectionchanged
iphone-web-app
mysql-error-1005
blitz++
ext3
thunderbird-lightning
compiler-specific
webkit.net
libs
sustainable-pace
procedural-music
defensive-programming
dbisam
caching-application-block





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