I'm trying to run a basic Kotlin main file but the problem is I don't have any run/debug configurations available.
My main.kt file is under src>main and is a very simple Helloworld program. See below image.
When I enter Add Configuration>Kotlin and type MainKt in Main Class, I get Warning: Class MainKt not found. In previous projects MainKt has been the default and I haven't had to add a configuration manually. Has anyone else come across this issue?
Image of project
Run/Debug Configuration image
Project Structure image
If I hit Run(in toolbar)>Run you can see no available configurations
EDIT: added Main module to source and MainKt class is now available in Add Run/Debug Configuration, but I get the error in below image, "Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt"
Error: Cannot find MainKt
The source for the class MainKt (main.kt) does not appear to be under your sources root (src/main/kotlin), it's one level up if I interpret the UI of intellij correctly. Try moving it into the kotlin/ directory.
Related
I just created a java project and ran it. It ran fine.
Then I converted it into a maven project and resolved all errors in POM.XMl file as well. When I try to run the class file , Am getting the error
Error: Could not find or load main class jhj
Caused by: java.lang.ClassNotFoundException: jhj
My class looks like this:
public class jhj {
public static void main(String[] args)
{
System.out.println("as");
}
}
And my Environment variables are properly set.
I have set the M2_home, MAVEN_HOME and JAVA_HOME as well.
I tried removing the project from the workspace and re-importing them.
Also, I tried with refresh and Maven>>update the project options as well
Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use.
For example, your web project's src/main/java should have target/classes under the web project, test classes should have target/test-classes also under the web project and so.
Using this configuration will allow you to execute unit tests in eclipse.
Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.
Hope it helps.
I spent the last 1,5 hour trying to make this simple tutorial work in IntelliJ IDEA, as you can see in this video.
When trying to run the code, I get the error:
/[...] -Dfile.encoding=UTF-8 src.HelloKt
Error: Could not find or load main class src.HelloKt
Caused by: java.lang.ClassNotFoundException: src.HelloKt
I have tried setting up SDK, invalidating cache, removing .idea and .gradle, rebuilding project, deleting the profile and adding it again. I tried those actions in different orders.
Here's a screenshot of the project:
It also complains Kotlin is not configured, but I have already configured it.
Here's the run configuration:
Here are the project settings:
Your Hello.kt file needs to be somewhere inside the src/main folder, probably in src/main/kotlin. This is different from the tutorial, because your project is using Gradle, and the one in the tutorial isn't. I think this is because newer versions of IntelliJ use Gradle by default for new projects, which wasn't the case when the tutorial was written.
The use of src/main/kotlin and src/test/kotlin as source code directories is a convention in Gradle (and Maven). When importing a Gradle project into IntelliJ, main becomes a module, and kotlin becomes a source folder within that module. The same goes for test. In your screenshots, the bold text and blue icons on main and test confirm that's how your project is set up. Files outside of those folders aren't treated as source files, which explains why your Hello.kt file isn't being compiled or recognised correctly.
It's likely that the default behaviour of IntelliJ when creating a new project has changed since this tutorial was written. In the tutorial, they select "Kotlin" as the project type and this creates a project that doesn't use Gradle. As a result, the project doesn't use the src/main/kotlin directory structure.
I can see from your video that you selected the same option, but on the next screen, IntelliJ still automatically selected Gradle as the build system for the new project. To match the project structure used in the tutorial, I think you would need to select "IntelliJ" as the build system.
I have a JavaFX project that I would like to build as a Jar-file. However, when I attempt to do so, I get an error.
Error:Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK
I found a similar problem on here from last year, but it seemed like they concluded nothing.
This happens because either you have many JDKs installed and compiling by another and running by another or you are using the Javafx Application jar feature when creating artifacts in Intellij which is unfortunately broken. Before proceeding with the below steps make sure that you are compiling with and running with the same JDK version.
Here is you fix it:
1 - Create a Launcher class:
The Launcher class is going to call the main JavaFx class from which your appliaction runs. Choosing to make the Jar directly through the Main class is going to error out giving the following error:
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
Your Launcher class should look something like this:
public class Launcher {
public static void main(String[] args) {
MainGUI.main(args);
}
}
2 - On to building the Jar
You probably still have a META-INF folder from the previous build so delete it.
Build the project as a JAR:
File->Project Structure -> Artifacts -> "+" -> JAR-> from modules with dependancies..
Choose the Launcher class for you main and check "copy to the output directory and link via Manifest" and Press Ok
Press Apply then OK.
go to Build -> Build artifacts-> Rebuild
In the JetBrains website I found a good article about, Package JavaFX applications which was really helpful. In the #troubleshoot section it says that,
Error:Java FX Packager: Can't build artifact – fx:deploy is not available in this JDK
The fx:deploy task was a part of the Ant plugin that was formerly
distributed in ant-javafx.jar as a part of Java Packager. The Ant
plugin is not included in jpackage in the current JDK versions.
If you're using a JDK build of version 9 and later, use third-party
solutions for packaging. For example, refer to section Runtime images
in the JavaFX official documentation.
I've got a Kotlin project in IntelliJ that is structured like this:
main/com/foo/Bar.kt
test/com/foo/BarTest.kt
where Bar is defined as:
internal class Bar
and BarTest is something like:
class BarTest {
private lateinit var bar: Bar
}
I'm getting compiler errors in IntelliJ on the reference to Bar with the message:
Cannot access 'Bar': it is internal in 'com.foo'
The tests, however, compile and run from the command line (via Gradle).
How can I suppress/remove this error when using IntelliJ?
My setup is:
macOS 10.14.1
IntelliJ 2018.2.6
Kotlin 1.3.10
I opened up two projects concurrently using the same IntelliJ installation:
a former project in which I was able to unit test an internal class located in the main source tree (all within the boundaries of the same IntelliJ module)
the current project in which I was unable to reproduce this setup
The fact that IntelliJ still gave no errors in the former setup led me to conclude the issue was not to do with my IntelliJ setup. As #JaysonMinard suggested, I looked at the differences in the Gradle configuration.
The difference was in the current project's top-level settings.gradle, I use the convention of renaming modules, a la:
findProject(':module-blah')?.name = 'blah'
...so as to refer to my Gradle projects by shorter names while having them listed contiguously in my project browser (rather than being scattered around with other files alphabetically). Removing this shortcut restored the behavior I was looking for in my unit tests.
First of all, i searched for similar questions and i found something, but nothing helps me out.
I'm trying to create a jar file in intellij using the artifact, but everytime i build i have the error: Error: Could not find or load main class com.test.wms.test
This is my test.java file
package com.test.wms;
public class test {
public static void main(String[] args){
// empty
}
}
Then i went into Project Structure -> Artifacts and added a new artifact, selected the type (jar), selected the name (test.jar) and generated the manifest.mf (autogenerated from the artifact page on intelij)
Manifest-Version: 1.0
Main-Class: com.test.wms.test
And this is the artifact edit page
Then i build the project and try to lunch in konsole with java -jar test.jar and the error is always the same: Error: Could not find or load main class com.test.wms.test
This is my project structure:
And this is my artifact edit page
Where is the mistake?
Thanks!
Fixed by myself, when i was creating the jar from the artifact i was selecting empty instead of From modules with dependancy (the output is the same but with this option works, maybe the IDE does something particular)