Android控制台中运行Java程序

测试代码(TCP通信服务端)

package com.demo;

import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 基于TCP协议的Socket通信
 * 服务器端
 */
public class Hello {
    public static void main(String[] args) {
        try {
            //1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket=null;
            //记录客户端的数量
            int count=0;

            System.out.println("\n***Start the server and wait for the client to connect***");

            //循环监听等待客户端的连接
            while(true){
                //调用accept()方法开始监听,等待客户端的连接
                socket=serverSocket.accept();

                //创建一个新的线程
                ServerThread serverThread = new ServerThread(socket);

                //启动线程
                serverThread.start();

                count++;//统计客户端的数量
                System.out.println("The number of clients: "+count);
                InetAddress address=socket.getInetAddress();
                System.out.println("Current client IP: "+address.getHostAddress());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /*
    * 服务器线程处理类
    */
    public static class ServerThread extends Thread {
        // 和本线程相关的Socket
        Socket socket = null;

        public ServerThread(Socket socket) {
            this.socket = socket;
        }

        //线程执行的操作,响应客户端的请求
        public void run(){
            InputStream       inputStream      = null;
            DataInputStream   dataInputStream  = null;
            OutputStream      outputStream     = null;
            DataOutputStream  dataOutputStream = null;
            try {
                //获取输入流,并读取客户端信息
                inputStream     = socket.getInputStream();
                dataInputStream = new DataInputStream(inputStream);

                //获取输出流,响应客户端的请求
                outputStream = socket.getOutputStream();
                dataOutputStream = new DataOutputStream(outputStream);

                while (true) {
                    //循环读取客户端的信息
                    byte b[] = new byte[1024];
                    b[0] = '\0';
                    dataInputStream.read(b);               
                    String info = new String(b);
                    System.out.println("I am a server and the client says: "+info);

                    if (info.trim().equals("quit")) {
                        break;
                    }

                    dataOutputStream.write("Welcome to visit!".getBytes("UTF-8"));
                    dataOutputStream.flush();//调用flush()方法将缓冲输出
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                //关闭资源
                try {
                    if(socket != null) {
                        socket.shutdownInput();//关闭输入流
                        socket.shutdownOutput();//关闭输出流
                    }
                    if(dataOutputStream != null) {
                        dataOutputStream.close();
                    }
                    if(outputStream != null) {
                        outputStream.close();
                    }
                    if (dataInputStream != null) {
                        dataInputStream.close();
                    }
                    if(inputStream != null) {
                        inputStream.close();
                    }
                    if(socket != null) {
                        socket.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



目录结构

源码:src/com/demo/Hello.java
编译后文件目录(和src同级):bin



编译

javac -sourcepath ./src ./src/com/demo/Hello.java -d ./bin  -encoding utf-8



将Java class转Android class

cd bin
dx --dex --output=Hello.dex ./com/demo/*.class



注意

如果出现以下异常信息,则表示jdk版本过高,而这个dx命令还不支持,如使用jdk版本是1.8,而这个dx命令还不支持1.8的jdk。那么需要重新编译。

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
        at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
        at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
        at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
        at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
        at com.android.dx.command.dexer.Main.processClass(Main.java:704)
        at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
        at com.android.dx.command.dexer.Main.access$300(Main.java:83)
        at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
        at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:170)
        at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
        at com.android.dx.command.dexer.Main.processOne(Main.java:632)
        at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
        at com.android.dx.command.dexer.Main.run(Main.java:246)
        at com.android.dx.command.dexer.Main.main(Main.java:215)
        at com.android.dx.command.Main.main(Main.java:106)
...while parsing com/demo/Hello.class

重新编译,并忽略警告信息,最后再重新将Java class转Android class

javac -sourcepath ./src -source 1.7 -target 1.7 ./src/com/demo/Hello.java -d ./bin -encoding utf-8



将Hello.dex推送到Android设备中

adb push Hello.dex /data/local/tmp



运行

export CLASSPATH=/data/local/tmp/Hello.dex
exec app_process /data/local/tmp com.demo.Hello

以上两条命令可以通过下面一条执行:

exec app_process -Djava.class.path=/data/local/tmp/Hello.dex /data/local/tmp com.demo.Hello 

你可能感兴趣的:(Java学习笔记,Android)