资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码模拟运行 JAVA模拟

用记事本写java代码怎么运行?

用记事本写完代码后运行方法如下:

我们提供的服务有:网站设计、成都网站设计、微信公众号开发、网站优化、网站认证、陵城ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的陵城网站制作公司

1、用浏览器打开用记事本编写的代码

新建“文本文档”后,鼠标右键点击该文本文档,在菜单栏的“打开方式”选择“用记事本打开”,也可以设置默认打开方式为“记事本”;用记事本打开文本文档后,直接在该文档内根据自己的需要输入想要编辑的网页代码。

2、记事本写java代码怎么运行

首先,需要安装jdk并配置环境变量。然后,在命令行中,用javac命令编译用记事本编写的代码。下一步,在命令行中,用java命令执行编译后的结果。

代码是什么

代码是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。代码设计的原则包括唯一确定性、标准化和通用性、可扩充性与稳定性、便于识别与记忆、力求短小与格式统一以及容易修改等。

计算机源代码最终目的是将人类可读文本翻译成为计算机可执行的二进制指令,这种过程叫编译,它由通过编译器完成。源代码就是用汇编语言和高级语言写出来的地代码。目标代码是指源代码经过编译程序产生的能被 cpu直接识别二进制代码。

可执行代码就是将目标代码连接后形成的可执行文件,当然也是二进制的。

用java实现一个模拟操作系统内核运行的程序。(1)进程控制:其中包括进程创建与撤销

在编写Java程序时,有时候需要在Java程序中执行另外一个程序。

1、启动程序Java提供了两种方法用来启动其它程序:

(1)使用Runtime的exec()方法

(2)使用ProcessBuilder的start()方法

不管在哪种操作系统下,程序具有基本类似的一些属性。一个程序启动后就程序操作系统的一个进程,进程在执行的时候有自己的环境变量、有自己的工作目录。Runtime和ProcessBuilder提供了不同的方式来启动程序,设置启动参数、环境变量和工作目录。

能够在Java中执行的外部程序,必须是一个实际存在的可执行文件,对于shell下的内嵌命令是不能直接执行的。

采用Runtime的exec执行程序时,首先要使用Runtime的静态方法得到一个Runtime,然后调用Runtime的exec方

法。可以将要执行的外部程序和启动参数、环境变量、工作目录作为参数传递给exec方法,该方法执行后返回一个Process代表所执行的程序。

Runtime有六个exec方法,其中两个的定义为:

public Process exec(String[] cmdarray, String[] envp, File dir)

public Process exec(String command, String[] envp, File dir)

cmdarray和command为要执行的命令,可以将命令和参数作为一个字符串command传递给exec()方法,也可以将命令和参数一个一个的方在数组cmdarray里传递给exec()方法。

envp为环境变量,以name=value的形式放在数组中。dir为工作目录。

可以不要dir参数,或者不要envp和dir参数,这样就多出了其它4个exec()方法。如果没有dir参数或者为null,那么新启动的

进程就继承当前java进程的工作目录。如果没有envp参数或者为null,那么新启动的进程就继承当前java进程的环境变量。

也可以使用ProcessBuilder类启动一个新的程序,该类是后来添加到JDK中的,而且被推荐使用。通过构造函数设置要执行的命令以及

参数,或者也可以通过command()方法获取命令信息后在进行设置。通过directory(File directory)

方法设置工作目录,通过environment()获取环境变量信息来修改环境变量。

在使用ProcessBuilder构造函数创建一个新实例,设置环境变量、工作目录后,可以通过start()方法来启动新程序,与Runtime的exec()方法一样,该方法返回一个Process对象代表启动的程序。

ProcessBuilder与Runtime.exec()方法的不同在于ProcessBuilder提供了

redirectErrorStream(boolean redirectErrorStream)

方法,该方法用来将进程的错误输出重定向到标准输出里。即可以将错误输出都将与标准输出合并。

2、Process

不管通过那种方法启动进程后,都会返回一个Process类的实例代表启动的进程,该实例可用来控制进程并获得相关信息。Process 类提供了执行从进程输入、执行输出到进程、等待进程完成、检查进程的退出状态以及销毁(杀掉)进程的方法:

(1) void destroy()

杀掉子进程。

一般情况下,该方法并不能杀掉已经启动的进程,不用为好。

(2) int exitValue()

返回子进程的出口值。

只有启动的进程执行完成、或者由于异常退出后,exitValue()方法才会有正常的返回值,否则抛出异常。

(3)InputStream getErrorStream()

获取子进程的错误流。

如果错误输出被重定向,则不能从该流中读取错误输出。

(4)InputStream getInputStream()

获取子进程的输入流。

可以从该流中读取进程的标准输出。

(5)OutputStream getOutputStream()

获取子进程的输出流。

写入到该流中的数据作为进程的标准输入。

(6) int waitFor()

导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。

通过该类提供的方法,可以实现与启动的进程之间通信,达到交互的目的。

3、从标准输出和错误输出流读取信息

从启动其他程序的Java进程看,已启动的其他程序输出就是一个普通的输入流,可以通过getInputStream()和getErrorStream来获取。

对于一般输出文本的进程来说,可以将InputStream封装成BufferedReader,然后就可以一行一行的对进程的标准输出进行处理。

4、举例

(1)Runtime.exec()

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStreamReader;

public class Test1 {

public static void main(String[] args) {

try {

Process p = null;

String line = null;

BufferedReader stdout = null;

//list the files and directorys under C:\

p = Runtime.getRuntime().exec("CMD.exe /C dir", null, new File("C:\\"));

stdout = new BufferedReader(new InputStreamReader(p

.getInputStream()));

while ((line = stdout.readLine()) != null) {

System.out.println(line);

}

stdout.close();

//echo the value of NAME

p = Runtime.getRuntime().exec("CMD.exe /C echo %NAME%", new String[] {"NAME=TEST"});

stdout = new BufferedReader(new InputStreamReader(p

.getInputStream()));

while ((line = stdout.readLine()) != null) {

System.out.println(line);

}

stdout.close();

} catch (Exception e) {

e.printStackTrace();

}

}

(2)ProcessBuilder

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Test2 {

public static void main(String[] args) {

try {

List list = new ArrayList();

ProcessBuilder pb = null;

Process p = null;

String line = null;

BufferedReader stdout = null;

//list the files and directorys under C:\

list.add("CMD.EXE");

list.add("/C");

list.add("dir");

pb = new ProcessBuilder(list);

pb.directory(new File("C:\\"));

p = pb.start();

stdout = new BufferedReader(new InputStreamReader(p

.getInputStream()));

while ((line = stdout.readLine()) != null) {

System.out.println(line);

}

stdout.close();

//echo the value of NAME

pb = new ProcessBuilder();

mand(new String[] {"CMD.exe", "/C", "echo %NAME%"});

pb.environment().put("NAME", "TEST");

p = pb.start();

stdout = new BufferedReader(new InputStreamReader(p

.getInputStream()));

while ((line = stdout.readLine()) != null) {

System.out.println(line);

}

stdout.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

5、获取进程的返回值

通常,一个程序/进程在执行结束后会向操作系统返回一个整数值,0一般代表执行成功,非0表示执行出现问题。有两种方式可以用来获取进程的返回

值。一是利用waitFor(),该方法是阻塞的,执导进程执行完成后再返回。该方法返回一个代表进程返回值的整数值。另一个方法是调用

exitValue()方法,该方法是非阻塞的,调用立即返回。但是如果进程没有执行完成,则抛出异常。

6、阻塞的问题

由Process代表的进程在某些平台上有时候并不能很好的工作,特别是在对代表进程的标准输入流、输出流和错误输出进行操作时,如果使用不慎,有可能导致进程阻塞,甚至死锁。

如果将以上事例中的从标准输出重读取信息的语句修改为从错误输出流中读取:

stdout = new BufferedReader(new InputStreamReader(p

.getErrorStream()));

那么程序将发生阻塞,不能执行完成,而是hang在那里。

当进程启动后,就会打开标准输出流和错误输出流准备输出,当进程结束时,就会关闭他们。在以上例子中,错误输出流没有数据要输出,标准输出流中

有数据输出。由于标准输出流中的数据没有被读取,进程就不会结束,错误输出流也就不会被关闭,因此在调用readLine()方法时,整个程序就会被阻

塞。为了解决这个问题,可以根据输出的实际先后,先读取标准输出流,然后读取错误输出流。

但是,很多时候不能很明确的知道输出的先后,特别是要操作标准输入的时候,情况就会更为复杂。这时候可以采用线程来对标准输出、错误输出和标准输入进行分别处理,根据他们之间在业务逻辑上的关系决定读取那个流或者写入数据。

针对标准输出流和错误输出流所造成的问题,可以使用ProcessBuilder的redirectErrorStream()方法将他们合二为一,这时候只要读取标准输出的数据就可以了。

当在程序中使用Process的waitFor()方法时,特别是在读取之前调用waitFor()方法时,也有可能造成阻塞。可以用线程的方法来解决这个问题,也可以在读取数据后,调用waitFor()方法等待程序结束。

总之,解决阻塞的方法应该有两种:

(1)使用ProcessBuilder类,利用redirectErrorStream方法将标准输出流和错误输出流合二为一,在用start()方法启动进程后,先从标准输出中读取数据,然后调用waitFor()方法等待进程结束。

如:

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Test3 {

public static void main(String[] args) {

try {

List list = new ArrayList();

ProcessBuilder pb = null;

Process p = null;

String line = null;

BufferedReader stdout = null;

//list the files and directorys under C:\

list.add("CMD.EXE");

list.add("/C");

list.add("dir");

pb = new ProcessBuilder(list);

pb.directory(new File("C:\\"));

//merge the error output with the standard output

pb.redirectErrorStream(true);

p = pb.start();

//read the standard output

stdout = new BufferedReader(new InputStreamReader(p

.getInputStream()));

while ((line = stdout.readLine()) != null) {

System.out.println(line);

}

int ret = p.waitFor();

System.out.println("the return code is " + ret);

stdout.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

(2)使用线程

import java.util.*;

import java.io.*;

class StreamWatch extends Thread {

InputStream is;

String type;

List output = new ArrayList();

boolean debug = false;

StreamWatch(InputStream is, String type) {

this(is, type, false);

}

StreamWatch(InputStream is, String type, boolean debug) {

this.is = is;

this.type = type;

this.debug = debug;

}

public void run() {

try {

PrintWriter pw = null;

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line = null;

while ((line = br.readLine()) != null) {

output.add(line);

if (debug)

System.out.println(type + "" + line);

}

if (pw != null)

pw.flush();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

public List getOutput() {

return output;

}

}

public class Test5 {

public static void main(String args[]) {

try {

List list = new ArrayList();

ProcessBuilder pb = null;

Process p = null;

// list the files and directorys under C:\

list.add("CMD.EXE");

list.add("/C");

list.add("dir");

pb = new ProcessBuilder(list);

pb.directory(new File("C:\\"));

p = pb.start();

// process error and output message

StreamWatch errorWatch = new StreamWatch(p.getErrorStream(),

"ERROR");

StreamWatch outputWatch = new StreamWatch(p.getInputStream(),

"OUTPUT");

// start to watch

errorWatch.start();

outputWatch.start();

//wait for exit

int exitVal = p.waitFor();

//print the content from ERROR and OUTPUT

System.out.println("ERROR: " + errorWatch.getOutput());

System.out.println("OUTPUT: " + outputWatch.getOutput());

System.out.println("the return code is " + exitVal);

} catch (Throwable t) {

t.printStackTrace();

}

}

}

7、在Java中执行Java程序

执行一个Java程序的关键在于:

(1)知道JAVA虚拟机的位置,即java.exe或者java的路径

(2)知道要执行的java程序的位置

(3)知道该程序所依赖的其他类的位置

举一个例子,一目了然。

(1)待执行的Java类

public class MyTest {

public static void main(String[] args) {

System.out.println("OUTPUT one");

System.out.println("OUTPUT two");

System.err.println("ERROR 1");

System.err.println("ERROR 2");

for(int i = 0; i args.length; i++)

{

System.out.printf("args[%d] = %s.", i, args[i]);

}

}

}

(2)执行该类的程序

import java.util.*;

import java.io.*;

class StreamWatch extends Thread {

InputStream is;

String type;

List output = new ArrayList();

boolean debug = false;

StreamWatch(InputStream is, String type) {

this(is, type, false);

}

StreamWatch(InputStream is, String type, boolean debug) {

this.is = is;

this.type = type;

this.debug = debug;

}

public void run() {

try {

PrintWriter pw = null;

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line = null;

while ((line = br.readLine()) != null) {

output.add(line);

if (debug)

System.out.println(type + "" + line);

}

if (pw != null)

pw.flush();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

public List getOutput() {

return output;

}

}

public class Test6 {

public static void main(String args[]) {

try {

List list = new ArrayList();

ProcessBuilder pb = null;

Process p = null;

String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";

String classpath = System.getProperty("java.class.path");

// list the files and directorys under C:\

list.add(java);

list.add("-classpath");

list.add(classpath);

list.add(MyTest.class.getName());

list.add("hello");

list.add("world");

list.add("good better best");

pb = new ProcessBuilder(list);

p = pb.start();

System.out.println(mand());

// process error and output message

StreamWatch errorWatch = new StreamWatch(p.getErrorStream(),

"ERROR");

StreamWatch outputWatch = new StreamWatch(p.getInputStream(),

"OUTPUT");

// start to watch

errorWatch.start();

outputWatch.start();

//wait for exit

int exitVal = p.waitFor();

//print the content from ERROR and OUTPUT

System.out.println("ERROR: " + errorWatch.getOutput());

System.out.println("OUTPUT: " + outputWatch.getOutput());

System.out.println("the return code is " + exitVal);

} catch (Throwable t) {

t.printStackTrace();

}

}

}

如何用java程序操作安卓模拟器

首先是电脑的java模拟器。在做测试以前在搜索引擎中将两个模拟器下载下来。自然是下载安全无毒的java模拟器。电脑使用的是exe文件但是常以rar打包。安卓智能机是apk文件。

我使用的java模拟器是个绿色软件,也就是说无需安装。解压压缩包后打开文件夹。找到主运行文件点击运行。

在程序主界面点击文件。选择载入jar文件。jar就是java数据文件java压缩包。山寨机时代大家经常接触。

我尝试加载一个很久前的javaQQ。载入之后自动运行程序。很熟悉的qq登陆界面。也尝试登陆了一下,果然无法登陆,没有在进行其他的检测。软件还提供了一些其他的功能诸如截图之类的比较实用。

如果是apk文件则先将其安装至手机安装成功之后再运行该应用。

软件界面空荡荡的,点击选项按钮,扫描内存卡。这时候软件会扫描手机内存卡中的jar文件。并返回搜索结果

点击该结果载入jar文件。这个jar就进入虚拟机管理列表中了。以后运行就可以直接在列表中运行。点击运行该jar等待程序执行即可。由于手机低配一时没有运行出结果。所以最终效果图就不贴了。

JAVA代码怎么运行

首先你的电脑上面必须安装java jdk ,默认安装就好, jdk分32位和64位,要下和你电脑系统一致的

然后配置java环境变量,网上有方法,容易找

然后下载eclipse软件,官网就有,上面两步完成的话eclipse解压就能用。eclipse位数也要和电脑系统一致

在eclipse中新建java代码,复制代码进去,然后点击run运行就行。

上面就是教你如何运行java代码了

用Java编写程序,设计一个模拟电梯运行的类

最佳答案代码如下:

public class ElevatorModel implenent ElevatorModelListener{

ElevatorShaft elevatorShaft;//电梯车箱

Floor firstFloor;

Floor secondFloor;

public void addPerson(Person person);

//实现ElevatorModelListener接口的所有方法

}

public abstract class Location{

String locationName;//可能占据的三个位置firstFloor; secondFloor;elevator中的其中一个位置。

public Button getButton();

public Door getDoor();

}

public class Floor extends Location{

public Floor(String firstFloor,String secondFloor);

public Button getButton();//具体实现

public Door getDoor();//具体实现

public ElevatorShaft getElevatorShaft();

}

public class Door implenent ElevatorMoveListener{

boolean open;//门是开的关的

public void openDoor();//开门

public viod clossDoor();//关门

}

public class Button implenent ElevatorMoveListener{

boolean pressed;

public void pressButton();//按下按钮

public void resetButton();//释放按钮

public void elevatorArrived();//电梯到来

}

public class ElevatorShaft{//电梯乘箱

Elevator elevator;//电梯

Button firstFloorButton,secondFloorButton;//出发层的按钮和到达层的按钮 Door firstFloorDoor,secondFloorDoor;//出发的门和到达的门

Light firstFloorLight,secondFloorLight;//灯

//监听器

DoorListener doorListener;

ButtonListener buttonListener;

LightListener ligthListener;

BellListener bellListener;

ElevatorMoveListener elevatorMoveListener;

Set set;

}

public class Light implenent ElevatorMoveListener{

boolean lightOn;//代表灯的状态,亮或灭

public void turnOnLight();

public void turnOffLight();

public void elevatorDeparted();//具体实现

public void elevatorArrived();//具体实现

}

public class Bell implenent ElevatorMoveListener{

public void ringBell();

public void elevatorDeparted();//具体实现

public void elevatorArrived();//具体实现

}

public class Elevator extends Location implenets

ButtonListener,DoorListener,BellListener,Runable

{//电梯乘箱

boolean moving;

boolean summoned;//描述Elevator是在移动还是已经补召唤

Floor currentFloor;//当前正在提供服务的Floor

Floor destinationFloor ; //代表将要到达的Floor

Button elevatorButton;

Door elevatorDoor;

Bell bell;

public void run();// 实现

public void requestElevator();//请求Elevator并产生排队请求。 }

public class Person extends Thread{

Location类对象(或是位于Floor上,或是出处在Elevator中); int ID;

boolean moving;

public void run();//具体实现

public void enterAndRideElevator();

}


网站栏目:java代码模拟运行 JAVA模拟
转载源于:http://cdkjz.cn/article/doojcoe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220