小编Dun*_*nes的帖子

Maven bundle插件 - 如何添加主类

我有一个Maven项目mjbean只有一个依赖项:TestA.这是mjbean的pom.xml:

<groupId>com.mbean</groupId>
<artifactId>mjbean</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging>

<build>
  <defaultGoal>install</defaultGoal>
  <plugins>
    <plugin>
      <groupId>org.apache.felix</groupId>
      <artifactId>maven-bundle-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <instructions>
          <Main-Class>com.mbean.Main</Main-Class>
          <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
          <Embed-Transitive>true</Embed-Transitive>
          <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
          <Import-Package>*</Import-Package>
        </instructions>
      </configuration>
    </plugin>
  </plugins>
</build>

<name>mjbean</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>com.testa</groupId>
    <artifactId>TestA</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

主要课程非常简单:

package com.mbean;
import com.testa.Testcl;
public class Main {

public static void main(String[] args) {

    Testcl tcl = new Testcl();
    tcl.testmethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

<Main-Class>com.mbean.Main</Main-Class>在maven-bundle-plugin中指定了主类.它与Eclipse运行良好.然后我使用Eclipse在目标文件夹中生成目标包.当我尝试在命令行中运行它时java -jar mjbean-0.0.1-SNAPSHOT.jar,我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/testa/Testcl
at com.mbean.Main.main(Main.java:12) …
Run Code Online (Sandbox Code Playgroud)

java eclipse maven maven-bundle-plugin

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

如何将 zip 文件读取为字节数组,然后将字节数组转换回 zip 文件?

我曾尝试使用此代码将 ZIP 文件转换为字节数组:

private static byte[] readZipFile(String zipFnm)
// read in fnm, returning it as a single string
{

  FileInputStream fileInputStream=null;

  File file = new File(zipFnm);

  byte[] bFile = new byte[(int) file.length()];

  try{
    //convert file into array of bytes
    fileInputStream = new FileInputStream(zipFnm);
    fileInputStream.read(bFile);
    fileInputStream.close();
  }catch(Exception e){
    e.printStackTrace();
  }
  return bFile;
}  
Run Code Online (Sandbox Code Playgroud)

以及用于将字节数组转换回 ZIP 的代码,通过调用 writeByteToZip(fnm + ".zip");

private static String writeByteToZip(String outFnm)
{
  try {
    FileOutputStream fileOuputStream = new FileOutputStream(outFnm); 
    fileOuputStream.write(bFile);
    fileOuputStream.close();            

  } catch ( IOException iox ){
    iox.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

java zip bytearray

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

禁用数据库连接的自动提交有什么影响?

如果我设置连接到手动提交是否意味着我的进程将锁定数据库?如果我必须执行可能需要4-5个小时才能执行的多个SQL查询,这是否意味着在此期间没有其他用户可以访问我的数据库?

我正在采用Web应用程序的场景,其中有成千上万的用户访问相同的数据库.

connection.setAutoCommit(false);
//multiple sql query that will probably take 4-5 hours to be executed

connection.commit();
Run Code Online (Sandbox Code Playgroud)

java sql

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

LDAP比较用户名密码?

我是 LDAP 新手,正在将其与com.sun.jndi.ldapjar 一起使用。

我有一个登录页面,用户在其中输入用户名和密码。我的工作是使用 LDAP 中的数据验证凭据。

到目前为止,我已经连接到 LDAP 服务器、验证并检索了 uid。

现在我想比较用户输入的密码和LDAP中的密码(密码是私有的,即在查询用户ID时无法查看它)。

有什么方法可以比较这两个密码吗?

java ldap

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

如果 getter 使用“is”而不是“get”,则 PropertyUtils 无法转换布尔值

我正在尝试使用 PropertyUtils 类的 copyProperties 方法来复制 bean。

问题在于,如果布尔值的 getter 写为“isXXX”,则无法复制布尔值。仅当布尔值的 getter 为“getXXX”时才有效。例如,

class MyBean {
....
    public boolean isEnabled() {
        return enabled;
    }
....
}
Run Code Online (Sandbox Code Playgroud)

PropertyUtils.copyProperties 不适用于此类。但它适用于此:

class MyBean {
....
    public boolean getEnabled() {
        return enabled;
    }
....
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法解决这一问题?

非常感谢

java apache-commons

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

在Java中测试并发

根据清单12.3中的Java Concurrency in Practice书,我们可以使用以下示例代码测试并发代码:

void testTakeBlocksWhenEmpty() {
 final BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
 Thread taker = new Thread() {
  public void run() {
   try {
    int unused = bb.take();
    fail(); // if we get here, it’s an error
   } catch (InterruptedException success) { }
  }
 };
 try {
  taker.start();
  Thread.sleep(LOCKUP_DETECT_TIMEOUT);
  taker.interrupt();
  taker.join(LOCKUP_DETECT_TIMEOUT);
  assertFalse(taker.isAlive());
 } catch (Exception unexpected) {
  fail();
 }
}
Run Code Online (Sandbox Code Playgroud)

假设执行了以下步骤:

  1. taker 线程启动。
  2. bb.take()成功返回,我们只需要一点点fail()方法就可以运行。
  3. 这称为interrupt()方法。
  4. 我们处于线程的catch障碍中taker

因此,我们目前处于困境,但实际上测试方法失败了。它失败了,我们从不知道。

这是正确的吗?如果是,我们该如何解决?

java testing concurrency multithreading blocking

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

Maven 不解析本地父 POM

这是一个有趣的问题,我不知道是我做错了什么还是 Maven 的限制。

该场景的最简单版本是有一个父 POM、单个子 POM 和一个聚合 POM。聚合 POM 只是将模块链接在一起。

当我安装聚合 POM 时,它没有找出子 POM 对父 POM 的依赖关系。我不希望有相对路径,根据我对 Maven 工件的理解应该是可能的。

任何见解将不胜感激。谢谢。

这是超级父级(别介意里面什么都没有)

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group</groupId>
    <artifactId>parent-uber</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>
Run Code Online (Sandbox Code Playgroud)

这是孩子:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>my.group</groupId>
        <artifactId>parent-uber</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </parent>

    <groupId>my.group</groupId>
    <artifactId>parent-java</artifactId>
    <packaging>pom</packaging>

    <properties>
    </properties>

    <build>
        <!-- To define the plugin version in your parent POM -->
        <pluginManagement>
            <plugins>
                <!-- All projects that extend this should have valid JavaDoc built-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions> …
Run Code Online (Sandbox Code Playgroud)

java pom.xml maven parent-pom

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

如何避免Java反射中的魔术字符串

我的应用程序中包含以下代码:

for(PropertyDescriptor property : myObjectProperties){
    if(property.getName().equals("myPropertyName")){
         // logic goes here
    }
}
Run Code Online (Sandbox Code Playgroud)

这当然在多个级别上都是危险的,可能最糟糕的是,如果我在“ MyObject”上重命名属性“ myPropertyName”,则代码将中断。

也就是说,在不显式键入属性的情况下引用属性名称的最简单方法是什么(因为这将使我得到编译器警告)?我看起来像:

for(PropertyDescriptor property : myObjectProperties){
    if(property.getName().equals(MyObject.myPropertyName.getPropertyName())){
         // logic goes here
    }
}
Run Code Online (Sandbox Code Playgroud)

还是Java甚至有可能?

java reflection

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

测试单个类时未调用@BeforeSuite

我有一个带@BeforeSuite注释的方法。

public class MySuiteTest {

    @BeforeSuite
    public static void doSomethingVeryMandatory() {
        // say, boot up an embedded database
        // and create tables for JPA-annotated classes?
    }
}

public class MySingleTest {

    @Test
    public void doSomething() {
        // say, tests some MyBatis mappers against the embedded database?
    }
}
Run Code Online (Sandbox Code Playgroud)

当我测试整个测试时,

$ mvn clean test
Run Code Online (Sandbox Code Playgroud)

一切安好。@BeforeSuite运行和@Test运行。

当我尝试测试一个班级时

$ mvn -Dtest=MySingleTest clean test
Run Code Online (Sandbox Code Playgroud)

doSomethingVeryMandatory() 不被调用。

这正常吗?

java testng unit-testing maven-surefire-plugin

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

获取当前请求URL的方案

在Ruby / Rack中,我可以从获取当前请求URL的方案scheme#request。但是,在Go中,http.Request.URL.Scheme返回一个空字符串:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%#v\n", r.URL.Scheme) // Always shows empty string
}
Run Code Online (Sandbox Code Playgroud)

如何获得当前请求URL的方案?

url http url-scheme go

5
推荐指数
4
解决办法
7806
查看次数