小编luc*_*uta的帖子

JUnit:NoClassDefFoundError:org/junit/runner/manipulation/Filter

当我尝试运行某些单元测试时,会引发以下错误:

java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:320)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:310)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:305)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:283)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:207)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:191)
Run Code Online (Sandbox Code Playgroud)

我必须提到junit-4.11.jar被添加到项目构建路径中.有任何想法吗?

java junit

16
推荐指数
2
解决办法
3万
查看次数

Java:get + clear atomic for map

我想实现以下逻辑:

- 使用以下结构

//Map<String, CopyOnWriteArrayList> keeping the pending updates 
//grouped by the id of the updated object
final Map<String, List<Update>> updatesPerId = new ConcurrentHashMap<>();
Run Code Online (Sandbox Code Playgroud)

-n producer将为updatesPerId map添加更新(对于相同的id,可以同时添加2个更新)

-one TimerThread将不时运行,并且必须处理收到的更新.就像是:

 final Map<String, List<Update>> toBeProcessed = new HashMap<>(updatesPerId);
 updatesPerId.clear();
 // iterate over toBeProcessed and process them
Run Code Online (Sandbox Code Playgroud)

有没有办法在不同步生产者的添加逻辑和timerThread(消费者)的逻辑的情况下使这个逻辑线程安全?我正在考虑一个原子清除+ get但似乎ConcurrentMap没有提供类似的东西.另外,我必须提到更新应该由更新的对象ID保存,因此我不能用队列或其他东西替换地图.

有任何想法吗?谢谢!

java collections concurrency

6
推荐指数
1
解决办法
210
查看次数

Java:getTimeZone 不返回默认值

我有以下说明:

TimeZone zone = TimeZone.getTimeZone("Asia/Toyo");
Run Code Online (Sandbox Code Playgroud)

显然,它应该返回 null,但它会返回默认时区,这不是我想要的行为。来自 Java 文档:

TimeZone如果无法理解给定的 ID,则返回指定的或 GMT 区域。

如果字符串不指示有效的时区,有没有办法获取相应的时区和空值?

我没有找到一个好的解决方案来获取所有时区并迭代它们。

java timezone

5
推荐指数
2
解决办法
1498
查看次数

获取Eclipse启动的进程的PID

我在eclipse中以调试模式启动了一个java程序,现在我需要其进程的PID才能生成Jmap快照.我尝试过:

ps aux | grep 'eclipse'
ps aux | grep 'myServerName'
Run Code Online (Sandbox Code Playgroud)

但没有机会找到那个PID.

java unix eclipse

5
推荐指数
1
解决办法
3738
查看次数

SimpleDateFormat行为

我有以下几行:

final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = simpleDateFormat.parse("2004-52-05");
Run Code Online (Sandbox Code Playgroud)

我希望第2行会抛出异常,因为'52'不是有效的月份,但是代码运行并且存储在d对象中的日期是

Sat Apr 05 00:00:00 EEST 2008
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下为什么吗?

java simpledateformat

4
推荐指数
1
解决办法
85
查看次数

计算圈复杂度的Java工具

我需要一个工具来计算给定类的方法的圈复杂度。另外,我需要从 Linux 命令行执行此操作。到目前为止,我阅读了 Source Monitor、Gmetrics 和 Sonar,但据我所知,这些工具不提供命令行使用。谢谢!

java linux metrics

3
推荐指数
1
解决办法
6299
查看次数

FindBugs:可能的空指针取消引用

嗨,我有以下方法:

protected boolean shouldCheckLimit() {
    if (startDate == null || endDate == null) {
        return true;
    }
    final Long currentTime = System.currentTimeMillis();
    if (startDate <= currentTime && currentTime < endDate) {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

问题是findBugs发现了以下问题:

Possible null pointer dereference of TimeIntervalLimit.startDate in com.bet.blues.limit.TimeIntervalLimit.shouldCheckLimit() [Scary(8), Normal 
Run Code Online (Sandbox Code Playgroud)

我必须提到startDate和endDate是Long变量.我尝试在条件中添加null的检查,我也尝试使用longValue()方法,但没有结果.你知道如何解决这个问题吗?可能是fndBugs方面的错误?

java findbugs

3
推荐指数
1
解决办法
1384
查看次数

TimeZone不了解EEST时区

我写了以下方法:

private static String getDate(final long addToCurrentMillis, final String pattern) {
    final long timeMilliseconds = System.currentTimeMillis() + addToCurrentMillis;
    final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    final Date resultdate = new Date(timeMilliseconds);
    final TimeZone timeZone = TimeZone.getDefault();
    sdf.setTimeZone(timeZone);
    return sdf.format(resultdate);
}
Run Code Online (Sandbox Code Playgroud)

我使用了"yyyy-MM-dd zzz"模式,结果将是:

2015-10-24 EEST

我正在使用此方法进行单元测试.问题是我尝试将此字符串转换回TimeZone我想要测试的代码,但TimeZone不知道EEST时区.有任何想法吗?

java timezone simpledateformat

3
推荐指数
1
解决办法
1678
查看次数

Gradle、shadowJar:在任务内使用重定位

我有以下任务:

task myJar(type: Jar) {
    archiveName = 'myJar.jar'
    includeEmptyDirs = false
    destinationDir = rootProject.libsDir
    dependsOn compileJava

    manifest.attributes('Class-Path': '../lib/commons-lang-2.5.jar')

    into '/', {
        from compileJava.destinationDir
        include 'com/myCompany/project/util/order/**',
                'com/myCompany/project/event/**',
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将所有类从 com/myCompany/project/event/** 重新定位到 com/myCompany/re located/project/event/** (以便某些应用程序使用我的 jar 并具有 com.myCompany.project.event 包定义将避免任何可能的冲突)

我发现可以使用影子插件来完成,我尝试添加

relocate 'com.myCompany.project.event.', 'com.myCompany.relocated.project.event.'
Run Code Online (Sandbox Code Playgroud)

在这个任务下,但它似乎不起作用。有谁知道我应该在哪里添加这一行?

gradle gradle-shadow-plugin

3
推荐指数
1
解决办法
8163
查看次数

synchronized增加一个int值

为什么这个程序在每次执行时都不会显示2000?我知道我可以使用AtomicInteger,但我很好奇.

class Increment extends Thread{
     static Integer i=new Integer(0);

    public void run(){

        for(int j=1;j<=1000;j++){
            synchronized (i) {
                i++;
            }
        }

    }
}


public class Puzzle {
    public static void main(String args[]) {    
        Thread t1=new Increment();
        Thread t2=new Increment();
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        }catch (InterruptedException r){}
        System.out.println(Increment.i);    
    }    
}
Run Code Online (Sandbox Code Playgroud)

java multithreading

2
推荐指数
1
解决办法
779
查看次数

在方法中使用@SuppressWarnings会生成编译错误

我有以下代码块:

     final Map<String, Double> map;
            if (cond) {
                int currency = 44;
                @SuppressWarnings("unchecked")
                map = (Map<String, Double>)objectA.get();
            }else {
                map= ....}
Run Code Online (Sandbox Code Playgroud)

get()objectA返回raw的方法HashMap(我知道在那里使用泛型会很好,我的问题会解决,但我无法更改该类的代码).如果我删除 @SuppressWarnings("unchecked")线路,显然我会收到TypeSafety警告.但是当我在线下添加supress警告时,我得到以下错误:

map cannot be resoved to a type!
Run Code Online (Sandbox Code Playgroud)

有人能解释我为什么吗?

java generics compiler-errors

1
推荐指数
1
解决办法
417
查看次数

Java:设置String.format不显示0位小数

有没有办法设置String.format,以便它不会显示00小数?例如,我想:

String.format("%.2f", 12.345d)
Run Code Online (Sandbox Code Playgroud)

显示12.34 但是

String.format("%.2f", 12.3d)
Run Code Online (Sandbox Code Playgroud)

显示12.3而不是12.30

java string string-formatting

1
推荐指数
1
解决办法
159
查看次数