Load an FXML form using JRuby - jruby

I am trying to load an FXML form from a JRuby script. I can successfully do that from a Jython script. This is the basic Jython code:
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.scene import Scene
class Main(Application):
def start(self, stage):
main_form = self.getClass().getResource('/main.fxml')
self.loader = FXMLLoader(main_form)
root = self.loader.load()
scene = Scene(root)
stage.setScene(scene)
stage.show()
args = []
print Main().class
print args
Application.launch(Main().class, args)
The script executes fine and the form gets shown. The output of the script is:
<type 'org.python.proxies.__main__$Main$0'>
[]
So Main.class() is type 'org.python.proxies.main$Main$0'
This is my attempt to create the equivalent JRuby script:
java_import javafx.application.Application
java_import javafx.fxml.FXMLLoader
java_import javafx.scene.Scene
class Main < Application
attr_accessor :loader, :fxml_form, :scene
def start(stage)
#fxml_form = self.java_class.getResource('/main.fxml')
#loader = FXMLLoader.new(#fxml_form)
root = #loader.load()
#scene = Scene.new(root)
stage.set_scene(#scene)
stage.show()
end
end
args = Array.new()
puts Main.new().java_class
puts args
Application.launch(Main.new().java_class, args)
The output of this script is:
javafx.application.Application
NameError: no method 'launch' for arguments (org.jruby.javasupport.JavaClass,org.jruby.RubyArray) on Java::JavafxApplication::Application
available overloads:
(java.lang.String[])
(java.lang.Class,java.lang.String[])
(root) at /home/uros/NetBeansProjects/JRuby-JavaXF/lib/main.rb:31
So Main.new().java_class is javafx.application.Application and the form never gets loaded. My FXML in both cases is a basic 'hello world' form:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="264.0" prefWidth="315.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" prefHeight="34.0" prefWidth="89.0" text="Button" />
</children>
</StackPane>
I am on Linux Mint 17. My Jython is 2.7b3 and my JRuby is 1.7.16.1.
[EDIT]
Additional information:
When trying to use the jrubyfx gem like this:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launch
I get the following exception:
NameError: cannot initialize Java class javafx.scene.control.ListView
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/core_ext/precompiled.rb:320
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
require at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55
glob at org/jruby/RubyDir.java:242
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:313
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:312
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx.rb:37
(root) at /home/uros/NetBeansProjects/RubyApplication4/lib/main.rb:1
My Java version:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)

NameError: cannot initialize Java class javafx.scene.control.ListView
was caused by
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
That JRubyFX error was fixed in 1.1.1 (which added java 8 support). 1.1.0 only supported Java 7 (after u6). It should work now using the code you gave in both 7u6+ and 8:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launch

Related

Issue with generating a PDF file using pdfkit in ruby rails application

I am trying to generate a basic PDF file with some data in it.
html = ApplicationController.new.render_to_string("invoices/invoice", layout: false)
kit = PDFKit.new(html, :page_size => 'Letter')
pdf = kit.to_file("#{Rails.root}/")
How to solve below error? or is there a better example that I use as a reference
2.7.4 :123 > html = ApplicationController.new.render_to_string("invoices/invoice", layout: false)
Rendering invoices/invoice.html.erb
Rendered invoices/invoice.html.erb (Duration: 0.0ms | Allocations: 4)
=> "<div style=\"width:100%;\">\n <p> Testing PDF Generation</p>\n</div>\n"
2.7.4 :124 > kit = PDFKit.new(html, :page_size => 'Letter')
=> #<PDFKit:0x0000000107ed4290 #source=#<PDFKit::Source:0x0000000107ed4268 #source="<div style=\"width:100%;\">\n <p> Testin...
2.7.4 :125 > pdf = kit.to_file("#{Rails.root}/")
/private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf: /private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf: cannot execute binary file
Traceback (most recent call last):
1: from (irb):125
PDFKit::ImproperWkhtmltopdfExitStatus (Command failed (exitstatus=126): /private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf --quiet --page-size Letter --margin-top 0.75in --margin-right 0.75in --margin-bottom 0.75in --margin-left 0.75in --encoding UTF-8 - /private/var/nameee/Desktop/test/test_application/)
More Info:
ruby 2.7.4p191
Rails 6.1.6.1
gem 'pdfkit', '~> 0.8.2'

module or name space not define error FS0039 [duplicate]

RaceConditionTest.fs
namespace FConsole
module RaceConditionTest =
let test x =
...
Program.fs
open System
open FConsole
[<EntryPoint>]
let main argv =
RaceConditionTest.test 1000
0 // return an integer exit code
Then I run my console app (linux)
$ dotnet run
error FS0039: The namespace or module 'FConsole' is not defined.
There is only one test method in RaceConditionTest.fs
Is the order of files the problem? if so, How do I indicate the order of *.fs files?
as #boran suggested in his comments there is this FConsoleProject.fsproj
I just added my file before Program.fs
<ItemGroup>
<Compile Include="RaceConditionTest.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Convert xml to json in Jenkinsfile

I have a problem with a method in my Jenkinsfile when i tried to convert xml to json. That is the method, and the pipeline.
I tried to pass the method directly to the echo, but it gives me an error and the pipeline fails
Sorry but i don't know that details i could give about the error, because i start to learning and this is the first time that i see this code.
ERROR: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 1; Content is not allowed in prolog.
I edit my question and i add a bat in stage OWASP dependencies testing. This bat create automatically the xml, i put in a validator xml and this did not errors. So i don't know if the problem is with the code of Jenkinsfile or xml, because the error is the same. I put part of xml code because it's very long, but
the error is still in the second line.
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<analysis xmlns="https://jeremylong.github.io/DependencyCheck/dependency-check.2.2.xsd">
<scanInfo>
<engineVersion>5.2.2</engineVersion>
<dataSource>
<name>NVD CVE Checked</name>
<timestamp>2019-11-25T09:01:51</timestamp>
</dataSource>
<datasource>...</datasource>
</scanInfo>
....................
</analysis>
import groovy.json.*;
def getDependencyResumeFromXML(pathReport){
def xml = bat(script:'type ' + pathReport, returnStdout:true);
def x = new XmlParser().parseText(xml);
def nDep = x.dependencies.dependency.size();
def dependencies = [:];
for(def i=0;i<nDep;i++){
dependencies[i] = [fileName: x.dependencies.dependency[i].fileName.text(),description:x.dependencies.dependency[i].description.text(),vulnerabilities:[:]];
def nVul = x.dependencies.dependency[i].vulnerabilities.vulnerability.size();
for(def j=0;j<nVul;j++){
dependencies[i].vulnerabilities[j] = [
name:x.dependencies.dependency[i].vulnerabilities.vulnerability[j].name.text(), cvssScore:x.dependencies.dependency[i].vulnerabilities.vulnerability[j].cvssScore.text(),
severity:x.dependencies.dependency[i].vulnerabilities.vulnerability[j].severity.text(),
cwe:x.dependencies.dependency[i].vulnerabilities.vulnerability[j].cwe.text(),
description:x.dependencies.dependency[i].vulnerabilities.vulnerability[j].description.text(),
];
}
}
return dependencies;
}
pipeline{
.......
stages{
stage('OWASP dependencies testing'){
steps{
script{
bat 'mvn org.owasp:dependency-check-maven:check';
def pathReport = 'C:\\tmp\\workspace\\umbrella-pipeline-prueba\\target\\dependency-check\\dependency-check-report.xml';
def xml = bat(script:'type ' + pathReport, returnStdout:true);
echo '------------------ 1';
echo xml;
echo '------------------ 2';
echo '--------------------------------'
def dependencias = getDependencyResumeFromXML(pathReport);
echo '------------- 3';
echo dependencias;
echo '------------- 4';
}
}
}
}
}

LuaLaTeX with LuaSQL on Archlinux

I am working on a LuaLaTeX file which would take data from my database using LuaSQL. So this are the 003-v1.tex and 003-v1.lua files that I came up with:
003-v1.tex file:
\documentclass{article}
\usepackage{luacode}
% Lua kodo vpišemo v ločeno datoteko zaradi syntax highlithing
\directlua{dofile('003-v1.lua')}
\newcommand{\stranke}{\luadirect{stranke()}}
\begin{document}
\begin{tabular}{ll}
\hline
id stranke & ime \\
\hline
\stranke
\hline
\end{tabular}
\end{document}
003-v1.lua file:
function stranke ()
package.cpath = package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so"
luasql = require "luasql.mysql"
env = assert (luasql.mysql())
con = assert (env:connect("linux_krozki","root","mypassword"))
cur = assert (con:execute("SELECT * FROM stranke"))
vnos = cur:fetch ({}, "a")
while vnos do
print(
string.format([[%s & %s \\]], vnos.id_stranke, vnos.ime)
)
vnos = cur:fetch (vnos, "a")
end
end
This files ought to work but when I try to compile using lualatex 003-v1.tex I get error:
This is LuaTeX, Version 1.0.4 (TeX Live 2017/Arch Linux)
restricted system commands enabled.
(./003-v1.tex
LaTeX2e <2017-04-15>
(using write cache: /home/ziga/.texlive/texmf-var/luatex-cache/generic)(using r
ead cache: /var/lib/texmf/luatex-cache/generic /home/ziga/.texlive/texmf-var/lu
atex-cache/generic)
luaotfload | main : initialization completed in 0.144 seconds
Babel <3.12> and hyphenation patterns for 1 language(s) loaded.
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo(load luc: /home/ziga/.texlive/
texmf-var/luatex-cache/generic/fonts/otl/lmroman10-regular.luc)))
(/usr/share/texmf-dist/tex/lualatex/luacode/luacode.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/share/texmf-dist/tex/luatex/luatexbase/luatexbase.sty
(/usr/share/texmf-dist/tex/luatex/ctablestack/ctablestack.sty))) (./003-v1.aux)
003-v1.lua:8: module 'luasql.mysql' not found:
no field package.preload['luasql.mysql']
[kpse lua searcher] file not found: 'luasql.mysql'
[kpse C searcher] file not found: 'luasql.mysql'
no file '/usr/local/lib/lua/5.2/luasql.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './luasql.so'
no file '/usr/lib/i386-linux-gnu/lua/5.1/luasql.so'
stack traceback:
[C]: in function 'require'
003-v1.lua:8: in function 'stranke'
[\directlua]:1: in main chunk.
\luadirect ... { \luacode#maybe#printdbg {#1} #1 }
l.14 \stranke
And according to this topic this error arrizes because LuaLaTeX can't load module luasql.mysql while lua can on its own. How do I know this? If I comment out first line (function stranke ()) and last line (end) from 003-v1.lua before compiling with lua 003-v1.lua I get an output which is completely fine:
1 & Žiga \\
2 & Ranja \\
3 & Romana \\
So my question is, how to make sure that module luasql.mysql loads when LuaLateX is called? I am on Archlinux and am using texlive. I heard that people compile the texlive again with support for luasql, but can't find the step by step guide... That would be awesome! It would be even better if there is anyone who already compiled it.
Here is the info about my Texlive version:
[ziga#laptop ~]$ pacman -Qs tex | grep live
local/texlive-bibtexextra 2017.44915-1 (texlive-most)
local/texlive-bin 2017.44590-2
local/texlive-core 2017.44918-1 (texlive-most)
local/texlive-fontsextra 2017.44818-1 (texlive-most)
local/texlive-formatsextra 2017.44177-2 (texlive-most)
local/texlive-games 2017.44131-1 (texlive-most)
local/texlive-humanities 2017.44833-1 (texlive-most)
local/texlive-langchinese 2017.44333-1 (texlive-lang)
local/texlive-langcyrillic 2017.44895-1 (texlive-lang)
local/texlive-langextra 2017.44908-1 (texlive-lang)
local/texlive-langgreek 2017.44917-1 (texlive-lang)
local/texlive-langjapanese 2017.44914-1 (texlive-lang)
local/texlive-langkorean 2017.44467-1 (texlive-lang)
local/texlive-latexextra 2017.44907-1 (texlive-most)
local/texlive-music 2017.44885-1 (texlive-most)
local/texlive-pictures 2017.44899-1 (texlive-most)
local/texlive-pstricks 2017.44742-1 (texlive-most)
local/texlive-publishers 2017.44916-1 (texlive-most)
local/texlive-science 2017.44906-1 (texlive-most)
We found an answer on Archlinux forums after this thread was posted. It looks like there are some internal problems with Lua language - package.cpath wasn't able to reckognize the questionmark, so ?.so had to be changed into mysql.so. Can anyone explain why this happened?

How to setup username and password with Slick's source code generator?

Following the directions in this page: http://slick.typesafe.com/doc/2.0.0/code-generation.html
we see that something like the following segment of code is required to generate models for mysql tables
val url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true"
val slickDriver = "scala.slick.driver.MySQLDriver"
val jdbcDriver = "com.mysql.jdbc.Driver"
val outputFolder = "/some/path"
val pkg = "com.pligor.server"
scala.slick.model.codegen.SourceCodeGenerator.main(
Array(slickDriver, jdbcDriver, url, outputFolder, pkg)
)
These parameteres are enough for an H2 database as the example in the link has it.
How to include username and password for the MySQL database?
From several links found in the internet and also based on the cvogt's answer this is the minimum that you need to do.
Note that this is a general solution for sbt. If you are dealing with play framework you might find it easier to perform this task with the relevant plugin
First of all you need a new sbt project because of all the library dependencies that are needed to be referenced in order for slick source generator to run.
Create the new sbt project using this tutorial: http://scalatutorials.com/beginner/2013/07/18/getting-started-with-sbt/
Preferably use the method Setup using giter8
If it happens to work with Intellij then you need to create file project/plugins.sbt and insert inside this line: addSbtPlugin("com.hanhuy.sbt" % "sbt-idea" % "1.6.0").
Execute gen-idea in sbt to generate an intellij project.
With giter8 you get an auto-generated file ProjectNameBuild.scala inside project folder. Open this and include at least these library dependencies:
libraryDependencies ++= List(
"mysql" % "mysql-connector-java" % "5.1.27",
"com.typesafe.slick" %% "slick" % "2.0.0",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"org.scala-lang" % "scala-reflect" % scala_version
)
where scala version is the variable private val scala_version = "2.10.3"
Now create the custom source code generator that looks like that:
import scala.slick.model.codegen.SourceCodeGenerator
object CustomSourceCodeGenerator {
import scala.slick.driver.JdbcProfile
import scala.reflect.runtime.currentMirror
def execute(url: String,
jdbcDriver: String,
user: String,
password: String,
slickDriver: String,
outputFolder: String,
pkg: String) = {
val driver: JdbcProfile = currentMirror.reflectModule(
currentMirror.staticModule(slickDriver)
).instance.asInstanceOf[JdbcProfile]
driver.simple.Database.forURL(
url,
driver = jdbcDriver,
user = user,
password = password
).withSession {
implicit session =>
new SourceCodeGenerator(driver.createModel).writeToFile(slickDriver, outputFolder, pkg)
}
}
}
Finally you need to call this execute method inside main project object. Find the file ProjectName.scala that was auto-generated by giter8.
Inside it you will find a println call since this is merely a "hello world" application. Above println call something like that:
CustomSourceCodeGenerator.execute(
url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true",
slickDriver = "scala.slick.driver.MySQLDriver",
jdbcDriver = "com.mysql.jdbc.Driver",
outputFolder = "/some/path",
pkg = "com.pligor.server",
user = "root",
password = "xxxxxyourpasswordxxxxx"
)
This way every time you execute sbt run you are going to generate the Table classes required by Slick automatically
Note that at least for 2.0.1 this is fixed. Just add username and password to the end of the Array as Strings
This has been asked and answered here: https://groups.google.com/forum/#!msg/scalaquery/UcS4_wyrJq0/obLHheIWIXEJ . Currently you need to customize the code generator. A PR for 2.0.1 is in the queue.
My solution is nearly the same as George's answer, but I'll add mine anyway. This is the entire file I use to generate code for my mysql database in an SBT project.
SlickAutoGen.scala
package mypackage
import slick.model.codegen.SourceCodeGenerator
object CodeGen {
def main(args: Array[String]) {
SourceCodeGenerator.main(
Array(
"scala.slick.driver.MySQLDriver",
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/mydb",
"src/main/scala/",
"mypackage",
"root",
"" // I don't use a password on localhost
)
)
}
}
build.sbt
// build.sbt --- Scala build tool settings
libraryDependencies ++= List(
"com.typesafe.slick" %% "slick" % "2.0.1",
"mysql" % "mysql-connector-java" % "5.1.24",
...
)
To use this, just modify settings, save in project root directory and run as follows:
$ sbt
> runMain mypackage.CodeGen