Can't import any java classes - ceylon

HelloWorld.ceylon
import java.util { HashMap } //Error:(1, 8) ceylon: package not found in imported modules: java.util (define a module and add module import to its module descriptor)
void run() {
print("test");
}
module.properties
module CeylonHelloWorld "1.0" {
import java.base "8";
}
I get an exception in HelloWord.ceylon file

When I try that code, I get:
Incorrect syntax: mismatched token CeylonHelloWorld expecting initial-lowercase identifier
In module.ceylon.
The name of a module is supposed to be of form foo.bar.baz (initial-lowercase identifiers separated by periods).

Like mentioned by Gavin you will have to use a legal module name, when I change your code to use the module name "java8test" I get the following output when compiling:
$ ceylon compile java8test
warning: It looks like you are using class files from a Java newer than 1.7.
Everything should work well, but if not, let us know at https://github.com/ceylon/ceylon-compiler/issues.
In the near future, the Ceylon compiler will be upgraded to handle Java 1.8.
./source/java8test/run.ceylon:1: warning: import is never used: 'HashMap'
import java.util { HashMap }
^
2 warnings
Note: Created module java8test/1.0.0
Which is all as expected.

module.ceylon
module holaCeylon "1.0.0"{
import java.base "7"; // versión 7 JDK
}
package.ceylon
shared package holaCeylon;
Now we go back to the run.ceylon file and import the java.util.HashMap Java library.
run.ceylon
import java.util { HashMap }
shared void run(){
print("Importando librerias de Java en Ceylon");
value romanos = HashMap<String,Integer>();
romanos.put("I", 1);
romanos.put("V", 5);
romanos.put("X", 10);
romanos.put("L", 50);
romanos.put("C", 100);
romanos.put("D", 500);
romanos.put("M", 1000);
print(romanos.values());
print(romanos.keySet());
}
Output:
salida
Code:
http://codemonkeyjunior.blogspot.mx/2015/03/ceylon-interoperabilidad-con-java.html

Related

Angular 6 with lowdb - not getting it to work

I'm trying to create an Electron app with Angular 6 that uses lowdb as a local database.
It's all very new to me and it's trial and error, but I don't seem to be able to figure out how to overcome the following error:
I've installed lowdb in my application by using the commands
npm install --save lowdb (edit: forgot to mention I did this already)
and
npm install --save #types/lowdb
I've created a service to communicate with this "local database".
import { Injectable } from '#angular/core';
import lowdb from 'lowdb';
import { default as FileAsync } from 'lowdb/adapters/FileAsync';
import { CurrentUserModel } from '../models/current-user';
#Injectable({
providedIn: 'root'
})
export class LowdbService {
private db: lowdb.LowdbAsync;
constructor() {
this.initDatabase();
}
set( field: string, value: any ) {
this.db.set( field, value ).write();
}
private async initDatabase() {
const adapter = new FileAsync( 'db.json' );
this.db = await lowdb( adapter );
this.db.defaults( { user: CurrentUserModel } );
}
}
But when I include the service in the constructor of a component I get errors.
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb' in '/Users/paul/Projects/application-name/src/app/services'
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb/adapters/FileAsync' in '/Users/paul/Projects/application-name/src/app/services'
ℹ 「wdm」: Failed to compile.
ERROR in src/app/services/lowdb.service.ts(2,12): error TS1192: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/index"' has no default export.
src/app/services/lowdb.service.ts(3,14): error TS2305: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/adapters/FileAsync"' has no exported member 'default'.
As far as I can see I'm doing the same as mentioned in this Github comment and this Stackoverflow comment. I can't find any more documentation though.
Can somebody help me out?
Update
Using import * as lowdb from 'lowdb' seemed to solve my initial errors. But it resulted in a few other errors. See below.
ERROR in ./node_modules/graceful-fs/polyfills.js
Module not found: Error: Can't resolve 'constants' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/graceful-fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/legacy-streams.js
Module not found: Error: Can't resolve 'stream' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
First of all, if you installed just #types/lowdb, that is only typings for that library, you need to install the library itself via:
npm install --save lowdb
Then, looks like there is no default export so import lowdb from 'lowdb'; also won't work. Try is like this:
import { LowdbAsync } from 'lowdb';
import * as lowdb from 'lowdb';
import * as FileAsync from 'lowdb/adapters/FileAsync';
private db: LowdbAsync<any>; // TODO provide correct schema instead of any
private async initDatabase() {
const adapter = new FileAsync('db.json');
this.db = await lowdb(adapter);
this.db.defaults({ user: any });
}
Or you can try removing those typings (as they might be outdated) and do what their docs says:
https://github.com/typicode/lowdb/tree/master/examples#browser
import low from 'lowdb'
import LocalStorage from 'lowdb/adapters/LocalStorage'
(but when you take a look at #types/lowdb, there is no such default export so again, the typings might be outdated)
As for your other issue, take a look here:
https://github.com/typicode/lowdb/issues/206#issuecomment-324884867
typicode commented on 25 Aug 2017
#DickyKwok I think it's impossible. Can you answer this, #typicode?
I confirm, to save to disk using FileSync or FileAsync adapters you need to have access to Node fs API.
Looks like you should use LocalStorage adapter instead.
Another similar issue here: How can I use node "fs" in electron within angular 5
You could also try the solution proposed in one of answers there:
https://github.com/ThorstenHans/ngx-electron
constructor(private _electronService: ElectronService) {
this.fs = this._electronService.remote.require('fs');
}
Looks like your lowdb is missing in node_modules.
try
npm install lowdb

How to import external JavaScript into Polymer 3.0

Let's say I have a traditional javascript library such as google-charts and I wanted to incorporate that into my Polymer 3.0 project. How would I actually do the import?
This is how I would traditionally do it: https://developers.google.com/chart/interactive/docs/quick_start
ES Modules on NPM
Normally, one would prefer an ES module version of such a library if available (e.g., on NPM), since ES modules allow tree shaking (by Polymer CLI in this case) to reduce the size of the production build output.
Modules are loaded in JavaScript with the import keyword. If available on NPM, the library could be installed in your Polymer project with the npm-install command. For instance, you'd install google-charts like this:
npm install --save google-charts
Then, import that package in your element code:
import { GoogleCharts } from 'google-charts';
Example:
import { PolymerElement } from '#polymer/polymer';
import { GoogleCharts } from 'google-charts';
class MyElement extends PolymerElement {
// ...
ready() {
super.ready();
GoogleCharts.load(/* ... */);
}
}
demo
Non-ES Modules (not on NPM)
On the other hand if ESM is unavailable, the script is likely an AMD/UMD module, which adds symbols to the global scope. In that case, you'd still import the file, and access the global as you normally would had you used the <script> tag.
If the script is not on NPM, you'd have to copy the file locally into your project (and include the library file in your release), and then import the file by path. For example, if you copied the library to <root>/src/libs/google-charts.js, the element code at <root>/src/components/Chart.html would import it like this:
import '../libs/google-charts'
Example:
import { PolymerElement } from '#polymer/polymer';
import '../libs/google-charts' // adds GoogleCharts to global
class MyElement extends PolymerElement {
// ...
ready() {
super.ready();
GoogleCharts.load(/* ... */);
}
}

Require HTML files as template String in Browserify when using tsify (Typescript)

I would like to require/import HTML templates as strings into my code. But running in some transpile errors.
This is the revelant Browserify setup I am using:
browserify({ basedir: './src' })
.transform(stringify(['.tpl.html']))
.add('app.ts')
.plugin(tsify)
In the app.ts I want to import a template like so:
import template from './app.tpl.html';
console.log(template);
Where app.tpl.html may look like this:
<h1>Hello!</h1>
I tried different setups with using require over import. Using require I get the following error:
Browserify Error { [TypeScript error: src/components/app.ts(1,9):
Error TS2304: Cannot find name 'require'.]
message: 'src/components/app.ts(1,9): Error TS2304: Cannot find name \'require\'.',
fileName: 'src/components/app.ts',
line: 1,
column: 9,
name: 'TypeScript error' }
Usind import the module is not found and I get the following error:
Browserify Error { [TypeScript error: src/components/test.ts(1,22): Error TS2307: Cannot find module './test.tpl.html'.]
message: 'src/components/test.ts(1,22): Error TS2307: Cannot find module \'./test.tpl.html\'.',
fileName: 'src/components/test.ts',
line: 1,
column: 22,
name: 'TypeScript error' }
I couldn't find an example using "stringify" and "tsify" together. Has anyone a working example who to use HTML templates toghether with "browserify" and "tsify"?
If you want to import/require in TypeScript something different than TypeScript module you need to use "native" require function (in this case to be defined by browserify). TypeScript compiler is not aware of that until you tell it.
declare function require (id: string): any; // declare there will be 'require' function in the runtime
var template = require('./app.tpl.html'); // use declared function in pure JS
Another option is to use <amd-depdendency> comment.
Code like this (menuTemplate.jst is static template file):
/// <amd-dependency path="general/menuTemplate.jst" name="menuTemplate" />
import Marionette = require("backbone.marionette");
declare var menuTemplate: string;
class MenuView extends Marionette.ItemView<any> {
template = menuTemplate;
}
produces output (ES 6):
/// <amd-dependency path="general/menuTemplate.jst" name="menuTemplate" />
define(["require", "exports", "general/menuTemplate.jst", "backbone.marionette"], function (require, exports, menuTemplate, Marionette) {
"use strict";
class MenuView extends Marionette.ItemView {
constructor(...args) {
super(...args);
this.template = menuTemplate;
}
}
});

Compile time error in ActionScript 3 project in FlashDevelop

I am trying to build a ActionScript side library for my SIP library Adobe AIR native extension following this blog from Adobe, in FlashDevelop IDE. When I build the project I get the following compile time error:
C:\Users\Osama
Mohammed\Documents\AndroidSIPManager\src\in\innovative\androidsipmanager\AndroidSIPManager.as(1):
col: 9 Error: Syntax error: expecting identifier before in.
I don't know why am I getting that error, although my syntax is right. I get this error when I write package name after package keyword in any ActionScript 3 project in ActionScript file, Eg. package my.package { ..., but don't get it when no package name is written after package keyword. Following is my AndroidSIPManager.as code:
package in.innovative.androidsipmanager //getting error here
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
/**
* ...
* #author Osama Mohammed Shaikh
*/
public class AndroidSIPManager extends EventDispatcher
{
private var extContext:ExtensionContext;
public function AndroidSIPManager(target:IEventDispatcher=null)
{
super(target);
extContext = ExtensionContext.createExtensionContext("in.innovative.SIPLibExtension", "sip");
if (!extContext)
{
throw new Error("SIP Library extension is not supported on this platform");
}
}
public function initialize(enum_transport:int, agent:String, STUNServer:String, STUNServerPort:int):int
{
return int (extContext.call("initialize", enum_transport, agent, STUNServer, STUNServerPort));
}
public function setUserInfo(userName:String, displayName:String, authName:String, password:String, localSIPPort:int, userDomain:String, SIPServer:String, SIPServerPort:int, outboundServer:String, outboundServerPort:int):int
{
return int (extContext.call("setUserInfo", userName, displayName, authName, password, localSIPPort, userDomain, SIPServer, SIPServerPort, outboundServer, outboundServerPort));
}
public function portSipCall(callee:String, enum_mediaType:int):Number
{
return Number (extContext.call("portSipCall", callee, enum_mediaType));
}
}
}
Please help me solve the problem.
Problem is that in is reserved word. This is the reason, why you're able to compile project after removing the package name completely.

Junit Category symbol not found

I can't get sample Junit 4.10 code with #Category annotation to compile. Code is:
import org.junit.*;
#Category({CatA.class})
public class A {
#Test
public void a() {
System.out.println("\n ***** Method a \n");
}
}
Command to compile code:
javac -cp .:junit-4.10.jar A.java
A.java:3: error: cannot find symbol
#Category({CatA.class})
^
symbol: class Category
1 error
Thanks,
Henry
You need the following import in your code as well:
import org.junit.experimental.categories.Category;