我尝试使用以下代码从公共时间服务器获取时间.
package aaa;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.net.TimeTCPClient;
public final class Main
{
public static java.util.Date getNTPDate() {
List<String> hosts = Arrays.asList("0.pool.ntp.org");
for (String host : hosts) {
TimeTCPClient client = new TimeTCPClient();
// We want to timeout if a response takes longer than 5 seconds
client.setDefaultTimeout(5000);
try {
client.connect(host);
java.util.Date ntpDate = client.getDate();
client.disconnect();
// Just to be extra caution.
if (ntpDate != null) {
return ntpDate;
}
}
catch (java.net.SocketException exp) {
exp.printStackTrace();
}
catch (java.io.IOException exp) …Run Code Online (Sandbox Code Playgroud) 我是Java的新手,也是JUnit测试的新手.这对我来说绝对清楚是什么Test类,但TestSuite班级让我感到困惑.有人可以解释一下TestSuite是为了什么吗?
我最近询问是否要报告getMessage()被捕异常的文本.相当令人惊讶的是,大多数答案误解了我的问题并认为我在询问是否报告了捕获异常的堆栈跟踪,这表明这样做被认为是常态.所以我要问一个跟进问题.
在您catch遇到异常时,您应该在哪种情况下或者不应该报告堆栈跟踪?通过"报告",我包括要求日志框架为您记录堆栈跟踪.
我不是在问一些事情.我在问这个报告是否应该包含堆栈跟踪.
我正在编写一个程序,将数据输出到.txt文件,可以由一个人使用NotePad等程序读取.也许不一定是ASCII,但用户可以理解的东西.
我使用哪一种?
我有这个赋值,要求我专门使用一个OutputStream子类,所以Writer不是一个选项.
课程概述
我试图在Eclipse Indigo中配置Tomcat 7,我将http://www.eclipsetotale.com/tomcatPlugin.html#A3中的Tomcat插件添加到eclipse\plugins文件夹中.现在,当我启动tomcat时,我收到以下错误:
Feb 13, 2012 4:23:35 PM org.apache.catalina.startup.ContextConfig webConfig
SEVERE: Unable to determine URL for WEB-INF/classes
javax.naming.NameNotFoundException: Resource /WEB-INF/classes not found
at org.apache.naming.resources.BaseDirContext.listBindings(BaseDirContext.java:733)
at org.apache.naming.resources.ProxyDirContext.listBindings(ProxyDirContext.java:546)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1197)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:825)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:300)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:897)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:873)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1617)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Feb 13, 2012 4:23:35 PM org.apache.coyote.AbstractProtocol start
INFO: …Run Code Online (Sandbox Code Playgroud) 到目前为止我有这个:
public static void main(String[] args) {
try {
String line;
Process p = Runtime.getRuntime().exec(
System.getenv("windir") + "\\system32\\" + "tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); // <-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
Scanner killer = new Scanner(System.in);
int tokill;
System.out.println("Enter PID to be killed: ");
tokill = killer.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
}
我希望能够根据用户输入的PID终止进程.我怎样才能做到这一点?(只需要在Windows上工作).*注意:必须能够杀死任何进程,inc.SYSTEM进程,所以如果使用taskkill.exe执行此操作,我猜测将需要-F标志?
所以,如果我有
Runtime.getRuntime().exec("taskkill /F /PID 827");
Run Code Online (Sandbox Code Playgroud)
如何用我的tokill变量替换"827"?
我正在用 Java 创建一个应用程序,我只想运行一个实例。为此,我创建了一个文件并在我的应用程序运行时获得了一个锁。
我有以下代码适用于 Windows,但在 Linux 上失败:一旦我获得了一个锁而不解锁它,我可以获得另一个锁。
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class MyApp {
private static File f;
private static FileChannel channel;
private static FileLock lock;
public static void main(String[] args) {
try {
f = new File("RingOnRequest.lock");
// Check if the lock exist
if (f.exists()) {
// if exist try to delete it
f.delete();
}
// Try to get the lock
channel = new RandomAccessFile(f, "rw").getChannel();
lock = channel.tryLock();
if(lock == null) …Run Code Online (Sandbox Code Playgroud) 如何配置Apache Cassandra以允许灾难恢复,以允许两个数据中心之一失败?
该DataStax文档有关使用,确保至少一个复制写入每个两个数据中心的复制策略的会谈.但是,一旦灾难真的发生,我看不出这有什么帮助.如果切换到剩余的数据中心,则所有写入都将失败,因为这些写入将无法复制到其他数据中心.
我想你希望你的软件能够在两种模式下运行:正常模式,写入必须跨数据中心复制,以及灾难模式,他们不需要.但似乎不可能改变复制策略.
我真正想要的是两个过度配置的数据中心,并且在正常操作期间使用两个数据中心的资源,但是当只有一个数据中心时,仅使用剩余数据中心的一个资源(性能降低)正在运作.
我们使用Spring MVC开发了一个标准的Java Web应用程序,并且最近尝试从3.0.6升级到3.2.0.几乎所有的servlet响应都是JSP或Json视图,但有一些是pdf请求,扩展名为"pdf".
在Spring 3.0.6中,我们有了这个设置,取自Spring MVC文档.
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="pdf" value="application/pdf"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,与XMLViewResolver结合使用.
更新到3.2.0后,出现故障:
Error creating bean with name' org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' defined in class path resource [dispatcher-test-servlet.xml]: Invocation of init method failed; nested exception is
java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType'
Run Code Online (Sandbox Code Playgroud)
在调查了文档和一些博客之后,这个配置似乎有效:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<list>
<!-- These are evaluated in order -->
<!-- Is there a media type based on suffix? -->
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map> …Run Code Online (Sandbox Code Playgroud) 我有以下主题:
public void start() {
isRunning = true;
if (mainThread == null) {
mainThread = new Thread(this);
mainThread.setPriority(Thread.MAX_PRIORITY);
}
if (!mainThread.isAlive()) {
try {
mainThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在某些时候我想停止它的操作:
public void stop() {
isRunning = false;
System.gc();
}
Run Code Online (Sandbox Code Playgroud)
start()再次调用时,抛出以下异常:
java.lang.IllegalThreadStateException
Run Code Online (Sandbox Code Playgroud)
指向mainThread.start()代码行.
启动/停止线程的最佳方法是什么?我怎样才能使这个线程可重用?
谢谢!