How can I execute a SBT managed JLine class - intellij-idea

I'm developing a JLine based application, which I'd obviously like to test as I develop.
JLine is a handy library which provides interactive console functionality to JVM applications.
JLine doesn't work in the Intellij console, probably because they've appropriated the tab key for their own nefarious needs, and what I want to test is tab-completion, because I'm implementing some tab-completed commands.
I drop to the SBT console, and try run-main Example simple, but I throws an Exception because there are now two jline libraries in the classloader - my one, and the one that SBT uses so the application explodes while loading JLine library (Singletons are evil)....
Sigh... Twiddle about at the SBT console for a bit, and discover I can run:
> show runtime:managed-classpath
[info] List(Attributed(/home/bryan/.sbt/boot/scala-2.10.0/lib/scala-library.jar), Attributed(/home/bryan/.ivy2/cache/org.parboiled/parboiled-scala_2.10/bundles/parboiled-scala_2.10-1.1.5.jar), Attributed(/home/bryan/.ivy2/cache/org.parboiled/parboiled-core/bundles/parboiled-core-1.1.5.jar), Attributed(/home/bryan/.ivy2/cache/jline/jline/jars/jline-2.10.jar))
I know I can parse that list, obviously spaces or commas would be a perfectly viable separator but Scala developers don't seem to be wired that way...
But SBT only seems to parse that command when I'm in it's console, if I execute that command from the actual, UNIX, console, like so:
% sbt show runtime:managed-classpath
[info] Loading project definition from /common/moon_excel/project
[info] Set current project to moon_excel (in build file:/common/moon_excel/)
[error] Not a valid command: show (similar: shell)
[error] Expected whitespace character
[error] Expected '/'
[error] Expected ':'
[error] Not a valid key: show (similar: show-timing)
[error] show
I'm trying to automate the process for when I've got 100 jars on the classpath (slight exaggeration), any suggestions?

sbt 0.13 (currently at RC3) moves JLine classes so that they aren't visible to user code. This should avoid conflicts with your code. Note that JLine currently leaks class loaders, so you may get PermGen errors after several runs.
You can use export runtime:fullClasspath in 0.13 to export a standard classpath string. In earlier versions, you can write a custom task. See also plugins like sbt-start-script, which generate a run script for you.
Finally, if possible, consider writing tests that don't need an interactive prompt. For example, sbt itself has some ScalaCheck properties for its completion library.

Related

What magic happens when IntelliJ compiles my Kotlin project?

I currently know that kotlinc hello.kt -include-runtime -d hello.jar compiles my hello world program written in kotlin. Furthermore I am able to execute my program via java -jar hello.jar.
Setting up a Kotlin "Hello world!" console application example in IntelliJ I may choose between different Build Systems (Maven, Gradle, IntelliJ).
I would like to know what happens in the background using the IntelliJ Build System for example?
It seems that there is an ant script running considering the Build Output:
Executing pre-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Running 'before' tasks
Checking sources
Kotlin: connecting to daemon
Kotlin: compiling [firstKotlinApp]
Kotlin: kotlinc-jvm 1.6.10-release-923 (JRE 17.0.1+12-39)
Kotlin: performing incremental compilation analysis
Checking dependencies… [firstKotlinApp]
Dependency analysis found 0 affected files
Updating dependency information… [firstKotlinApp]
Running 'after' tasks
Finished, saving caches…
Executing post-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Synchronizing output directories…
04.01.22, 07:59 - Build completed successfully in 6 sec, 686 ms
Can someone help me out and direct me to the right place to look?
I already tried to find information with several search engines.

Running a simple hello world from the console after installing IntelliJ IDEA

I have installed IntelliJ IDEA on my mac and wrote the simplest Kotlin program
fun main(args : Array<String>){
println("Hello")
}
I can run it from the IDE environment. (It prints Hello of course)
My question: How can you run this from the console?
What I have done:
I tried to call
java simplekt.class
but I got
Error: could not find or load main class simplekt.class
I tried java simplekt but then I got an exception in thread main java.lang.NoClassDefFoundError
I tried to use kotlin or kotlinc but the command was not found. (where is the compiler installed?)
In this resource they use kotlinc and they produce a jar file but IDEA only output a class file.
Not really sure how to proceed from here.
When you run your application from the IDE, in the Run window the very first line is the command that the IDE executes to start your program. In my case it's something like:
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 <a lot more omitted>
You can execute the same command in your terminal and that will execute the application. Reading that line will also (indirectly) tell you where the kotlinc command is installed, and in my case – using MacOS – it's at /Applications/IntelliJ\ IDEA.app/Contents/plugins/Kotlin/kotlinc/bin/kotlinc
However, you can always decide to entirely stop using the IDE and compile/run your program from the command line by following instructions here: https://kotlinlang.org/docs/tutorials/command-line.html
The above answer by #user2340612 is mostly right, but maybe due to software changing or some differences in what i did, it did not work for me. Following did:
to avoid repetition, i'll be using the short name: $(ideac) = "IntelliJ IDEA Community Edition 2022.2" in place of the full name
the path to the kotlinc is this one: $(ideac)\plugins\Kotlin\kotlinc\bin
much similar to the one told above, but slightly different
i have verified it to work in the command given in the command-line doc linked above or compiler-reference too: kotlinc hello.kt -include-runtime -d hello.jar
running the resultant .jar file via java -jar ./hello.jar shows the expected output
there's no kotlin-native in that folder, so, couldn't verify this native-command-line-compiler kotlin doc
Backstory/What did not work
the command under the run tab in $(ideac) no longer shows the kotlinc path
I had created the Kotlin "project" by using "new project" (intelliJ) or by "Kotlin multiplatform > JVM" (gradle)
The run command for either of these did not contain any kotlinc in them
I tried creating a new > "scratch" file; and it showed this: $(ideac)\plugins\Kotlin\bin\windows\LLDBFrontend.exe but i tried using that on CLI and it did not work either

When running a custom TestEngine can execution of JUnitTestEngine be suppressed?

I have created a custom TestEngine using the JUnit 5 (junit-platform-engine) framework.
The custom TestEngine registers using the ServiceLoader mechanism with an entry in META-INF/services/org.junit.platform.engine.TestEngine.
When I run my tests, this works well, but the tests get run a second time by the built-in JUnitTestEngine.
Is it possible to replace the default TestEngine in this circumstance instead of supplement it?
After checking the JUnit 5 user guide and documentation for maven surefire plugin it seems there's currently no way to filter out certain test engine with Maven :-(.
Using the console launcher, however, does allow to choose test engines: https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher-options. And so does Gradle: https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle.

Do not start Intellij Scala Compile Server while running

I start my play multi module application as a SBT Task using something like:
";project testProject;~run"
It starts without a problem but after a couple of code reloads I would get something like
java.lang.IllegalArgumentException: requirement failed: Source file '/Users/testUser/git/test/modules/commons/target/scala-2.11/classes.bak/sbt3506893140703367490.class' does not exist.
at scala.Predef$.require(Predef.scala:233)
at sbt.IO$.copyFile(IO.scala:636)
at sbt.IO$.move(IO.scala:839)
at sbt.inc.ClassfileManager$$anonfun$transactional$1$$anon$2$$anonfun$complete$5.apply(ClassfileManager.scala:66)
After googling I found this Problems with Compiling Play Application
If I manually stop the scala compile server in Intellij then the problem goes away. Thing is the compile server starts automatically everytime I run the SBT Task. Is there a way to disable this?

Griffon resource loading differences between run-app and test-app

I am fairly new to Griffon and have some experience with Grails.
I have a problem loading a file from the resources directory.
I am using Griffon version 1.4.0.
When I run griffon run-app the following code (inside a Service) to get the location of an XML file works fine:
URL resource = getResourceAsURL('schema.xsd')
assert resource != null : "schema cannot be located"
When I run griffon test-app however the same code produces an assertion error because the returned URL is null. Same behaviour with getResourceAsStream().
This happens in the unit test of said service.
I put the file in ./griffon-app/resources.
What am I doing wrong? Do I have to copy all resources from production to some test resources folder, do I have to edit the build-configuration?
Thanks in advance!
Edit as suggested below I filed a bug report in the griffon-projects issue tracker.
araxn1d is correct, running the tests in integration mode will give you the right answer because the full application gets bootstrapped before tests are run. Now, running this kind of test (a unit test that depends on resources being available in the classpath) encounters a problem because the classpath is not setup correctly. Executing the following command
griffon -Dgriffon.cli.verbose=true test-app --unit --compileTrace=true
will output all classpaths. There you can see that the resources directory points to $USER_HOME/.griffon/1.4.0/projects/<project_name>/resources. If you inspect that directory you'll find the file you're looking for inside griffon-app/resources. This means the test classpath is not accurately configured as it should be $USER_HOME/.griffon/1.4.0/projects/<project_name>/resources/griffon-app/resources instead. This is clearly a bug, most likely found in the $GRIFFON_HOME/scripts/_GriffonClasspath.groovy script. Could you please file a JIRA http://jira.codehaus.org/browse/griffon ticket for it? Thanks!
You should run test-app to run your unit tests. In this case you should mock any refers to real files, otherwise you should implement Integration Tests. Pls see Griffon Testing. Integration tests differ from unit tests in that you have full access to the Griffon application within the test.