所以我正在尝试编写我的第一个从网络上提取数据的程序,因为它是一个财务程序,我决定使用(据称)简单易用的Yahoo!财务API。我下载了API .jar文件,只是为了保存,我通过Add External Jars工具将它们全部导入Eclipse。
问题是,每当我尝试运行它时,我都会收到SocketException:Permission Denied runtime error。
我正在运行的代码非常基本:
Stock stock = YahooFinance.get("ORCL");
这一行代码应该从雅虎财经获得Oracle(ORCL)股票的价值。相反,它会抛出异常。
我已经尝试了我能想到的一切:
1)我禁用了防火墙 2)我仔细检查了我的.jar导入 3)我测试了将这一行作为一个单独的线程的一部分运行(一个绝望的,盲目的移动,但我正在达到这一点)。
我已经厌倦了我能想到的每一个可能的研究角度,到目前为止我发现的只有:Sending email using JSP
这看起来像一个非常类似的问题,所以我尝试了建议的解决方案:在我的eclipse.ini文件中添加“-Djava.net.preferIPv4Stack = true”行。它似乎没有效果。
以防万一,我也在这里粘贴我的eclipse.ini文件的内容,以防我把它放在错误的地方或者其他地方:
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20140603-1326
-product
org.eclipse.epp.package.standard.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Djava.net.preferIPv4Stack=true
-Dosgi.requiredJavaVersion=1.7
-Xms40m
-Xmx512m
我现在没有想法。我只是想知道为什么会抛出异常,以及我可以做些什么来阻止它。
提前致谢。
编辑:按要求,完整堆栈跟踪。
Apr 27, 2015 11:54:35 PM yahoofinance.quotes.QuotesRequest getResult
INFO: Sending request: http://finance.yahoo.com/d/quotes.csv?s=ORCL&f=nsc4xab2sa5sbb3sb6sl1sk3sd1t1opghva2kjm3m4sj2sss1sj1sf6sr1qdyee7e9e8rr5p6p5b4s6j4t8&e=.csv
Apr 27, 2015 11:54:35 PM yahoofinance.quotes.QuotesRequest getResult
SEVERE: java.net.SocketException: Permission denied: connect
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at yahoofinance.quotes.QuotesRequest.getResult(QuotesRequest.java:83)
at yahoofinance.YahooFinance.getQuotes(YahooFinance.java:332)
at yahoofinance.YahooFinance.get(YahooFinance.java:76)
at yahoofinance.YahooFinance.get(YahooFinance.java:61)
at currency.CurrencyConverter.createWindow(CurrencyConverter.java:35)
at currency.CurrencyConverter.<init>(CurrencyConverter.java:25)
at currency.CurrencyDriver$1.run(CurrencyDriver.java:10)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:-1)
我认为我遇到了同样的问题:有时需要一点点努力,但我设法让我想要的信息有点无聊,我的意思是,递归:
void searchQuote(symbol) throws InterruptedException {
try{
Stock stock = YahooFinance.get(symbol);
}catch(Exception e){
if(e.getClass().getName().equals("java.net.SocketException") ||
e.getClass().getName().equals("java.net.SocketTimeoutException")){
Thread.sleep(60000);
searchQuote(symbol);
}
}
}
对我来说,它有效。