I'm trying to run a .class file compiled through G...
# announcements
m
I'm trying to run a .class file compiled through Gradle, just for the sake of it but I haven't had any success. I want to use
java
, how can I make it run? I've tried stuff like this:
Copy code
$ java -cp "/usr/share/kotlin/lib/kotlin-stdlib.jar:." kott.AppKt     
Error: Could not find or load main class kott.AppKt
Caused by: java.lang.ClassNotFoundException: kott.AppKt

$ java AppKt 
Error: Could not find or load main class AppKt
Caused by: java.lang.NoClassDefFoundError: kott/AppKt (wrong name: AppKt)
e
classpath
.
implies
kott.AppKt
is can be found at
./kott/AppKt.class
. is it?
☝️ 1
a
Are you sure you have kott directory where you are starting and AppKt class (or App file)
m
Copy code
/home/manuel/dev/kott/app/build/classes/kotlin/main/kott> ls
App.class  AppKt.class
e
Java only works with the correct directory structure
go up one directory and try again
a
^
m
you mean my working directory?
e
yes
a
/home/manuel/dev/kott/app/build/classes/kotlin/main
here
e
Java is expecting to load
kott.AppKt
from the file
./kott/AppKt.class
. if it is actually at
./AppKt.class
it will not be found.
m
Copy code
/home/manuel/dev/kott/app/build/classes/kotlin/main> java AppKt
Error: Could not find or load main class AppKt
Caused by: java.lang.ClassNotFoundException: AppKt
I dunno what I'm doing wrong yet
e
java kott.AppKt
no shortcuts
a
Why'd you removed the
kott
m
oh right
Copy code
/home/manuel/dev/kott/app/build/classes/kotlin/main> java <http://kott.App|kott.App>
Error: Main method not found in class <http://kott.App|kott.App>, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
okay it works now (except it doesn't, but I know why this doesn't work) thanks for the help
e
if you use Gradle's
application
plugin, it can generate a startup script that encapsulates all the JAVA_HOME and classpath manipulation
a
Kotlin usually generate
{FileName}Kt
file encapsulating the top-level main function.
You can rename it through
@file:JvmName("...")
e
not just main but any top-level declarations (e.g. outside of a class or interface). because Java requires everything belong to a class
and yes, although of course
@file:
annotations have to precede all other content in the file (except whitespace and comments, those are allowed)
m
Oh yeah I actually landed on here minutes before I made this thread, and I checked the docs too
I kept doing stupid mistakes because I'm not used to the compiler at all
its CLI, that is
and some details about Java
a
Just use the kotlin jupyter kernel if you like to build and test only a few code, it'll be alot more productive. Or else use the gradle to build your project.