编写一个applet,在浏览器中打开本机代码。当我从IDE作为文件运行它时,它可以工作,但是当我使用glassfish作为本地主机从浏览器运行它时,代码会抛出异常。
package rootapplet;
import java.awt.*;
import java.io.File;
public class RootApplet extends javax.swing.JApplet{
int number;
private File file;
public void init(){
//test code
number = 42;
}
public void start(){
new Exception().printStackTrace();
try{
File f = new File("C:\\Windows\\System32");
ProcessBuilder pb = new ProcessBuilder("cmd","/c","start","Notepad.exe");
pb.directory(f);
Process process = pb.start();
//ProcessBuilder derp = new ProcessBuilder("C:\\Windows\\System32\\Notepad.exe","myfile.txt");
//derp.start();
System.out.println("Words words words");
}
catch(Exception e){
System.out.println("Stuff didn't work");
}
}
public void paint (Graphics screen){
Graphics screen2D = (Graphics2D) screen;
screen2D.drawString("The square root of "+number+" is "+Math.sqrt(number),5,50);
}
}
我在代码中留下了另一个ProcessBuilder方法,而不是编码意义,因为ProcessBuilders都有效。但是当我从localhost运行它时,每个问题都会出现同样的问题。我假设它是一个IO异常,我只是没有将正确的文件路径传递给applet。如果是这样的话,有谁知道我应该通过它而不是我的意思?如果没有,可能会发生什么?
堆栈跟踪
java.lang.Exception
at rootapplet.RootApplet.start(RootApplet.java:18)
Words Words Words
at sun.applet.AppletPanel.run(AppletPanel.java:475)
at java.lang.Thread.run(Thread.java:745)
Java控制台输出
security: Certificate validation succeeded using OCSP/CRL
security: Saving certificates in Deployment session certificate store
security: Saved certificates in Deployment session certificate store
security: Checking if certificate is in Internet Explorer TrustedPublisher certificate store
network: Created version ID: 1.0+
network: Created version ID: 1.0
security: Expected Main URL: http://localhost:8080/classes/rootapplet/RootApplet.class
network: Cache entry found [url: http://localhost:8080/classes/rootapplet/RootApplet.class, version: null] prevalidated=false/0
cache: Adding MemoryCache entry: http://localhost:8080/classes/rootapplet/RootApplet.class
cache: Resource http://localhost:8080/classes/rootapplet/RootApplet.class has expired.
network: Connecting http://localhost:8080/classes/rootapplet/RootApplet.class with proxy=DIRECT
network: Connecting http://localhost:8080/ with proxy=DIRECT
network: ResponseCode for http://localhost:8080/classes/rootapplet/RootApplet.class : 304
network: Encoding for http://localhost:8080/classes/rootapplet/RootApplet.class : null
network: Disconnect connection to http://localhost:8080/classes/rootapplet/RootApplet.class
network: Cache entry not found [url: http://localhost:8080/classes/, version: null]
security: Grant socket perm for http://localhost:8080/classes/ : java.security.Permissions@452267 (
("java.net.SocketPermission" "localhost" "connect,accept,resolve")
)
network: Created version ID: 1.7.0.60
network: Created version ID: 1.7.0.65
0
basic: Embedding dialogs not enabled in Configuration
network: Created version ID: 1.7.0.60
network: Created version ID: 1.7.0.65
security: SSV validation:
running: 1.7.0_55
requested: 1.7.0_55
range: null
javaVersionParam: 1.7.0_55
Rule Set version: null
network: Created version ID: 1.7.0.55
network: Created version ID: 1.7.0.55
security: continue with running version
network: Created version ID: 1.7.0.55
network: Created version ID: 1.7
network: Created version ID: 2.2.55
security: --- parseCommandLine converted :
into:
[]
basic: Applet loaded.
basic: Applet resized and added to parent container
basic: PERF: AppletExecutionRunnable - applet.init() BEGIN ; jvmLaunch dt 930624 us, pluginInit dt 6880938 us, TotalTime: 7811562 us
basic: Applet initialized
basic: Starting applet
basic: completed perf rollup
Stuff didn't work again
basic: Applet made visible
basic: Applet started
basic: Told clients applet is started
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/blacklist
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/baseline.version
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/blacklisted.certs
network: Connecting https://javadl-esd-secure.oracle.com/update/blacklist with proxy=DIRECT
network: Connecting https://javadl-esd-secure.oracle.com/update/baseline.version with proxy=DIRECT
network: Connecting https://javadl-esd-secure.oracle.com/update/blacklisted.certs with proxy=DIRECT
network: Cache entry not found [url: file:/C:/Program%20Files%20(x86)/Java/jre7/lib/ext/sunec.jar, version: null]
network: Cache entry not found [url: file:/C:/Program%20Files%20(x86)/Java/jre7/lib/ext/sunjce_provider.jar, version: null]
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
security: Loading Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loaded Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loading SSL Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loaded SSL Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
security: Loading certificates from Internet Explorer ROOT certificate store
security: Loaded certificates from Internet Explorer ROOT certificate store
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
network: Updating file at: C:\Users\USER_NAME\AppData\LocalLow\Sun\Java\Deployment\security\baseline.versions from url: https://javadl-esd-secure.oracle.com/update/baseline.version
network: Updating file at: C:\Users\USER_NAME\AppData\LocalLow\Sun\Java\Deployment\security\blacklisted.certs from url: https://javadl-esd-secure.oracle.com/update/blacklisted.certs
network: Created version ID: 1.7.0.60
network: Created version ID: 1.7.0.65
network: Updating file at: C:\Users\USER_NAME\AppData\LocalLow\Sun\Java\Deployment\security\blacklist.dynamic from url: https://javadl-esd-secure.oracle.com/update/blacklist
network: Created version ID: 1.7.0.60
network: Created version ID: 1.7.0.65
答案 0 :(得分:1)
您看到的堆栈跟踪不是由抛出的异常生成的,而是由您在start()的第一行中构造的异常生成的:
new Exception().printStackTrace();
与该堆栈跟踪混合的输出“Words Words Words”仅可见,因为抛出了 no 异常并且执行了try块的最后一行。
在您的控制台输出中,有一行“Stuff not not again again”,这不是由您的来源生成的。
出于调试目的,扩展catch-block可能会有所帮助:
catch(Exception e){
System.out.println("Stuff didn't work");
e.printStackTrace
}
也许删除令人不快的行“new Exception()。printStackTrace();”
答案 1 :(得分:1)
我找到了自己的解决方案。我不得不将它作为一个jar包装起来,并使用我调用它的HTML中的applet标签中的archive标签来运行它。