顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2017年5月15日 星期一

2017 在 Ubuntu 16.04 上面報稅

沒有最爛只有更爛的報稅系統,Java 連官方都已經不支援使用 Java Applet 建議改用 Java Web Start 了,但是臺灣政府配合的廠商還是在使用過時的技術給 Mac & Linux 系統的報稅者,主管機關也是一直瀆職。

目前 Ubuntu 上的 Firefox 53.0.2 已經不支援 Java Applet 了,所以想要報稅只能安裝舊版本的 Firefox 來避開問題。

首先確認 Firefox 已經都關閉,然後將 ~/.mozilla 換個名稱或是備份起來,以防意外的發生。

$ mv ~/.mozilla ~/.mozilla.bak

接下來要安裝舊版本的 Firefox

$ apt policy firefox
firefox:
  已安裝:53.0.2+build1-0ubuntu0.16.04.2
  候選: 53.0.2+build1-0ubuntu0.16.04.2
  版本列表:
 *** 53.0.2+build1-0ubuntu0.16.04.2 500
        500 http://tw.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu xenial-security/main amd64 Packages
        100 /var/lib/dpkg/status
     45.0.2+build1-0ubuntu1 500
        500 http://tw.archive.ubuntu.com/ubuntu xenial/main amd64 Packages
$ sudo apt install firefox=45.0.2+build1-0ubuntu1 firefox-locale-zh-hant=45.0.2+build1-0ubuntu1

然後就是依照先前寫過的「在 Ubuntu 16.04 上面使用自然人憑證報稅」來操作,就可以順利報稅了。

2008年9月24日 星期三

利用程式碼註解來達成 #if #else #endif 的效果

下面這段 C 語言的程式碼
#include <stdio.h>

int main()
{
    //*/
    printf("If\n");
    /*/
    printf("Else\n");
    //*/
    return 0;
}
效果相當於
#include <stdio.h>

int main()
{
#if 1
    printf("If\n");
#else
    printf("Else\n");
#endif
    return 0;
}
如果把第一個註解改成 /*/ 後
#include <stdio.h>

int main()
{
    /*/
    printf("If\n");
    /*/
    printf("Else\n");
    //*/
    return 0;
}
效果就相當於
#include <stdio.h>

int main()
{
#if 0
    printf("If\n");
#else
    printf("Else\n");
#endif
    return 0;
}
這個小技巧也可以應用在其他可以使用 // /**/ 當作註解的程式語言

像是 Java 沒有 macro 可以使用,遇到 JNI 這類的東西,系統上就一定要安裝相關的 native library 才可以使用 JNI,如果在 JNI 上面加上一層空殼再搭配這樣的技巧就可以快速地在 native function 與 pseudo function 之間切換。

改寫之前寫過的 JNI - Java Native Interface
class HelloWorld
{
    // If '/*/' use native, '//*/' use pseudo.
    //*/
    public int[][] multiply(int A[][], int B[][]) { return null; }
    /*/
    public native int[][] multiply(int A[][], int B[][]);
    static
    {
        System.loadLibrary("HelloWorldImp");
    }
    //*/

    public static void main(String[] args)
    {
        HelloWorld test = new HelloWorld();
        int[][] A = new int[5][5];
        int[][] B = new int[5][5];
        int[][] result = test.multiply(A, B);
        System.out.println(result[0][0]);
    }
}
如果只是要開發一些跟 JNI 功能無關的 Java 程式部份,又不想要安裝或是無法安裝那些相依的 native library 的時候就很方便了。 ^o^

2007年7月5日 星期四

Java 的外部命令

import java.io.*;
...
try {
Process p = Runtime.getRuntime().exec("ls");
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
} catch (IOException e) {
...
}
...

基本又實用... 先記下來... :P

2007年7月3日 星期二

JNI - Java Native Interface

The Java Native Interface (JNI) is a programming framework that allows Java code running in the Java virtual machine (VM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages, such as C, C++ and assembly.
... from the link

HelloWorld.java
class HelloWorld
{
public native int[][] multiply(int A[][], int B[][]);
static
{
System.loadLibrary("HelloWorldImp");
}

public static void main(String[] args)
{
HelloWorld test = new HelloWorld();
int[][] A = new int[5][5];
int[][] B = new int[5][5];
int[][] result = test.multiply(A, B);
System.out.println(result[0][0]);
}
}

然後使用 javac HelloWorld.java 產生出 HelloWorld.class
接下來再使用 javah HelloWorld 產生出 HelloWorld.h

HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */

#ifndef __HelloWorld__
#define __HelloWorld__

#include

#ifdef __cplusplus
extern "C"
{
#endif

JNIEXPORT jobjectArray JNICALL Java_HelloWorld_multiply (JNIEnv *env, jobject, jobjectArray, jobjectArray);

#ifdef __cplusplus
}
#endif

#endif /* __HelloWorld__ */

再把 HelloWorld.c 寫出來
HelloWorld.c
#include "HelloWorld.h"

JNIEXPORT jobjectArray JNICALL Java_HelloWorld_multiply (JNIEnv *env, jobject obj, jobjectArray A, jobjectArray B);
{
jobjectArray C;
...
return C;
}

最後使用 gcc -Wall -g -shared -fPIC HelloWorld.c -o libHelloWorldImp.so
現在終於可以執行 Java 程式了
java -Djava.library.path=./ HelloWorld
使用 Java 來做跨平台程式開發
在關鍵部份再利用 JNI 來將平台特性最佳化

2007年5月19日 星期六

使用 Linux 來報稅就是二等公民嗎!?

又到了報稅的五月了~
每年要報稅時總是要裝一次 Java Runtime Environment
因為報稅的網站需要裝 Java Runtime Environment 才能夠操作... !@#$%^&*()
還好在 Ubuntu Linux 7.04 底下安裝也不是多麻煩的事
sudo aptitude install sun-java6-plugin
裝是裝好了... 總算是可以開始填資料了吧...
tax
哇哩勒... !@#$%^&*()
有種讓人想要砸機器的衝動... >"<
媽媽樂勒... 使用 Linux 來報稅就是二等公民嗎!?

2005年9月12日 星期一

Hello Java!

已經好久好久都沒有寫過 Java 的程式了
週末剛好在 GNU/Linux 上面試著寫個 Hello Java!

// Hello.java
class Hello {
public static void main(int argc, string[] args) {
System.out.println("Hello Java!");
}
}

$ gcj -C Hello.java
Hello.java:2: error: Type 'string' not found in the declaration of the argument 'args' of method 'main'.
public static void main(int argc, string[] args) {
^
1 error

疑~ 是哪裡寫錯了嗎!? @.@a
原來在 Java 上的 String 開頭要大寫啊...XD

// Hello.java
class Hello {
public static void main(int argc, String[] args) {
System.out.println("Hello Java!");
}
}

$ gcj -C Hello.java
$ gij Hello
no suitable method `main' in class

哇哩勒~ 原來 Java 不用加上 int argc 啊...XD
// Hello.java
class Hello {
public static void main(String[] args) {
System.out.println("Hello Java!");
}
}

$ gcj -C Hello.java
$ gij Hello
Hello Java!

這次總算對了吧...嘿嘿... ^__^
試試看直接編成 native binary executable file

$ gcj Hello.java
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

吼~ 這次又是怎麼回事啊... XD
原來是要加上 --main 啊... >"<

$ gcj --main=Hello Hello.java
$ ./a.out
Hello Java!

耶! 果然成功了~~~ ^__^y
後來還發現可以這樣用

$ gcj Hello.java -lgij
$ ./a.out
Usage: gij [OPTION] ... CLASS [ARGS] ...
to invoke CLASS.main, or
gij -jar [OPTION] ... JARFILE [ARGS] ...
to execute a jar file
Try `gij --help' for more information.

錯! 應該要這樣用! XD

$ ./a.out Hello
Hello Java!

久久沒用 Java 寫程式還真的是忘了一堆東西耶... Orz