草庐IT

java - 当我不能时,Maven 正在执行什么样的魔法来运行这个项目?

coder 2023-08-28 原文

我有一个带有一些库依赖项 (.dll) 的 Maven 项目(我将其放在“lib”文件夹中)。我可以在 Netbeans 中毫无问题地运行项目,但是当我尝试在 Netbeans 之外运行构建的 .jar 时,加载库时出现以下错误:

无法在 AMD 64 位平台上加载此 .dll(机器代码=0xbd)

我的计算机上只安装了 一个 Java 实例,它应该与 Netbeans/Maven 用来运行项目的 JVM 相同。所以我不明白 Netbeans/Maven 如何能够在与我不同的平台上运行这个应用程序?我试过查看 Netbeans 执行的命令(从输出中)来运行项目,我认为是这样的:

cd C:\Users\Birger\Workspace\myproject; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_91" cmd /c "\"\"C:\\Program Files\\NetBeans 8.1\\java\\maven\\bin\\mvn.bat\" -Dexec.args=\"-Djava.library.path=lib\\ -classpath %classpath com.mysite.myproject.Main\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk1.8.0_91\\bin\\java.exe\" -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.1\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""

我试过这两个命令

"C:\Program Files\Java\jdk1.8.0_91\jre\bin\java.exe" -Djava.library.path=lib\ -jar myproject-1.0-SNAPSHOT.jar
"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" -Djava.library.path=lib\ -jar myproject-1.0-SNAPSHOT.jar

我添加了 System.out.println(System.getProperty("sun.arch.data.model")); 让我的应用程序打印出 cpu 架构。它在两种情况下都打印 64

尝试查看 C:\Program Files\NetBeans 8.1\java\maven\bin\mvn.bat 中的“mvn.bat”文件,但我找不到任何关于什么的线索Maven 正在运行我的应用程序。

有人可以帮我解决这个问题吗?

伯杰

编辑

这是我的测试项目的完整源代码。我项目的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mysite</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <repositories>
        <repository>
            <id>repo</id>
            <url>file://${project.basedir}/temp-repo</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>jni4net</groupId>
            <artifactId>jni4net.j</artifactId>
            <version>0.8.8.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.mysite.myproject.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <resources>          
                                <resource>
                                    <directory>lib</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>              
                        </configuration>            
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我的项目的类

package com.mysite.myproject;

import java.io.File;
import java.io.IOException;
import net.sf.jni4net.Bridge;

public class Main {

    static {
        String libDir = System.getProperty("java.library.path");
        System.loadLibrary("jni4net.n-0.8.8.0");

        if (System.getProperty("sun.arch.data.model").equals("64")) {
            System.loadLibrary("jni4net.n.w64.v20-0.8.8.0");
            System.loadLibrary("jni4net.n.w64.v40-0.8.8.0");
        } else {
            System.loadLibrary("jni4net.n.w32.v20-0.8.8.0");
            System.loadLibrary("jni4net.n.w32.v40-0.8.8.0");
        }

        try {
            Bridge.init(new File(libDir));
            System.out.println("Initialized!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        System.out.println("Hello world!");
    }
}

运行项目时的 Netbeans 输出(为详细输出添加了 --debug 选项):

cd C:\Users\Birger\Workspace\myproject; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_91" cmd /c "\"\"C:\\Program Files\\NetBeans 8.1\\java\\maven\\bin\\mvn.bat\" -Dexec.args=\"-Djava.library.path=lib\\ -classpath %classpath com.mysite.myproject.Main\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk1.8.0_91\\bin\\java.exe\" -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.1\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 --debug org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
C:\Program Files\Java\jdk1.8.0_91
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: C:\Program Files\NetBeans 8.1\java\maven
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_91\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
Populating class realm maven.ext
  Included C:\Program Files\NetBeans 8.1\java\maven-nblib\netbeans-eventspy.jar
Error stacktraces are turned on.
Reading global settings from C:\Program Files\NetBeans 8.1\java\maven\conf\settings.xml
Reading user settings from C:\Users\Birger\.m2\settings.xml
Using local repository at C:\Users\Birger\.m2\repository
Using manager EnhancedLocalRepositoryManager with priority 10 for C:\Users\Birger\.m2\repository
Scanning for projects...
Extension realms for project com.mysite:myproject:jar:1.0-SNAPSHOT: (none)
Looking up lifecyle mappings for packaging jar from ClassRealm[maven.ext, parent: ClassRealm[plexus.core, parent: null]]
=== REACTOR BUILD PLAN ================================================
Project: com.mysite:myproject:jar:1.0-SNAPSHOT
Tasks:   [org.codehaus.mojo:exec-maven-plugin:1.2.1:exec]
Style:   Regular
=======================================================================

------------------------------------------------------------------------
Building myproject 1.0-SNAPSHOT
------------------------------------------------------------------------
Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]
Lifecycle clean -> [pre-clean, clean, post-clean]
Lifecycle site -> [pre-site, site, post-site, site-deploy]
=== PROJECT BUILD PLAN ================================================
Project:       com.mysite:myproject:1.0-SNAPSHOT
Dependencies (collect): []
Dependencies (resolve): [test]
Repositories (dependencies): [libs-release (http://artifactory.osc.no:8081/artifactory/libs-release/, releases), repo (file://C:\Users\Birger\Workspace\myproject/temp-repo, releases+snapshots), central (http://repo.maven.apache.org/maven2, releases)]
Repositories (plugins)     : [libs-release (http://artifactory.osc.no:8081/artifactory/libs-release/, releases), central (http://repo.maven.apache.org/maven2, releases)]
-----------------------------------------------------------------------
Goal:          org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli)
Style:         Regular
Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <basedir default-value="${basedir}"/>
  <classpathScope default-value="runtime">${exec.classpathScope}</classpathScope>
  <commandlineArgs>${exec.args}</commandlineArgs>
  <executable>${exec.executable}</executable>
  <longClasspath default-value="false">${exec.longClasspath}</longClasspath>
  <outputFile>${exec.outputFile}</outputFile>
  <project default-value="${project}"/>
  <session default-value="${session}"/>
  <skip default-value="false">${skip}</skip>
  <sourceRoot>${sourceRoot}</sourceRoot>
  <testSourceRoot>${testSourceRoot}</testSourceRoot>
  <workingDirectory>${exec.workingdir}</workingDirectory>
</configuration>
=======================================================================
com.mysite:myproject:jar:1.0-SNAPSHOT
   jni4net:jni4net.j:jar:0.8.8.0:compile

--- exec-maven-plugin:1.2.1:exec (default-cli) @ myproject ---
Created new class realm maven.api
Importing foreign packages into class realm maven.api
  Imported: org.apache.maven.cli < maven.ext
  Imported: org.codehaus.plexus.lifecycle < maven.ext
  Imported: org.apache.maven.lifecycle < maven.ext
  Imported: org.apache.maven.repository < maven.ext
  Imported: org.codehaus.plexus.personality < maven.ext
  Imported: org.apache.maven.usability < maven.ext
  Imported: org.codehaus.plexus.configuration < maven.ext
  Imported: org.sonatype.aether.version < maven.ext
  Imported: org.sonatype.aether.* < maven.ext
  Imported: org.sonatype.aether.artifact < maven.ext
  Imported: org.apache.maven.* < maven.ext
  Imported: org.apache.maven.project < maven.ext
  Imported: org.sonatype.aether.repository < maven.ext
  Imported: org.sonatype.aether.impl < maven.ext
  Imported: org.apache.maven.exception < maven.ext
  Imported: org.apache.maven.plugin < maven.ext
  Imported: org.sonatype.aether.collection < maven.ext
  Imported: org.codehaus.plexus.* < maven.ext
  Imported: org.codehaus.plexus.logging < maven.ext
  Imported: org.apache.maven.profiles < maven.ext
  Imported: org.sonatype.aether.metadata < maven.ext
  Imported: org.sonatype.aether.spi < maven.ext
  Imported: org.codehaus.plexus.util.xml.pull.XmlPullParserException < maven.ext
  Imported: org.apache.maven.wagon.* < maven.ext
  Imported: org.sonatype.aether.graph < maven.ext
  Imported: org.apache.maven.rtinfo < maven.ext
  Imported: org.sonatype.aether.installation < maven.ext
  Imported: org.apache.maven.monitor < maven.ext
  Imported: org.sonatype.aether.transfer < maven.ext
  Imported: org.codehaus.plexus.context < maven.ext
  Imported: org.apache.maven.wagon.observers < maven.ext
  Imported: org.apache.maven.wagon.resource < maven.ext
  Imported: org.sonatype.aether.deployment < maven.ext
  Imported: org.apache.maven.model < maven.ext
  Imported: org.codehaus.plexus.util.xml.Xpp3Dom < maven.ext
  Imported: org.apache.maven.artifact < maven.ext
  Imported: org.apache.maven.toolchain < maven.ext
  Imported: org.codehaus.plexus.util.xml.pull.XmlSerializer < maven.ext
  Imported: org.apache.maven.settings < maven.ext
  Imported: org.apache.maven.wagon.authorization < maven.ext
  Imported: org.apache.maven.wagon.events < maven.ext
  Imported: org.apache.maven.wagon.authentication < maven.ext
  Imported: org.apache.maven.reporting < maven.ext
  Imported: org.apache.maven.wagon.repository < maven.ext
  Imported: org.apache.maven.configuration < maven.ext
  Imported: org.codehaus.plexus.classworlds < maven.ext
  Imported: org.codehaus.classworlds < maven.ext
  Imported: org.codehaus.plexus.util.xml.pull.XmlPullParser < maven.ext
  Imported: org.apache.maven.classrealm < maven.ext
  Imported: org.sonatype.aether.resolution < maven.ext
  Imported: org.apache.maven.execution < maven.ext
  Imported: org.apache.maven.wagon.proxy < maven.ext
  Imported: org.codehaus.plexus.container < maven.ext
  Imported: org.codehaus.plexus.component < maven.ext
Populating class realm maven.api
org.codehaus.mojo:exec-maven-plugin:jar:1.2.1:
   org.apache.maven:maven-toolchain:jar:1.0:compile
   org.apache.maven:maven-project:jar:2.0.6:compile
      org.apache.maven:maven-settings:jar:2.0.6:compile
      org.apache.maven:maven-profile:jar:2.0.6:compile
      org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
   org.apache.maven:maven-model:jar:2.0.6:compile
   org.apache.maven:maven-artifact:jar:2.0.6:compile
   org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
      org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
   org.apache.maven:maven-core:jar:2.0.6:compile
      org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.6:compile
      org.apache.maven.reporting:maven-reporting-api:jar:2.0.6:compile
         org.apache.maven.doxia:doxia-sink-api:jar:1.0-alpha-7:compile
      org.apache.maven:maven-error-diagnostics:jar:2.0.6:compile
      commons-cli:commons-cli:jar:1.0:compile
      org.apache.maven:maven-plugin-descriptor:jar:2.0.6:compile
      org.codehaus.plexus:plexus-interactivity-api:jar:1.0-alpha-4:compile
      org.apache.maven:maven-monitor:jar:2.0.6:compile
      classworlds:classworlds:jar:1.1:compile
   org.apache.maven:maven-plugin-api:jar:2.0.6:compile
   org.codehaus.plexus:plexus-utils:jar:2.0.5:compile
   org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:compile
      junit:junit:jar:3.8.2:test (scope managed from compile) (version managed from 3.8.1)
   org.apache.commons:commons-exec:jar:1.1:compile
Created new class realm plugin>org.codehaus.mojo:exec-maven-plugin:1.2.1
Importing foreign packages into class realm plugin>org.codehaus.mojo:exec-maven-plugin:1.2.1
  Imported:  < maven.api
Populating class realm plugin>org.codehaus.mojo:exec-maven-plugin:1.2.1
  Included: org.codehaus.mojo:exec-maven-plugin:jar:1.2.1
  Included: org.apache.maven.reporting:maven-reporting-api:jar:2.0.6
  Included: org.apache.maven.doxia:doxia-sink-api:jar:1.0-alpha-7
  Included: commons-cli:commons-cli:jar:1.0
  Included: org.codehaus.plexus:plexus-interactivity-api:jar:1.0-alpha-4
  Included: org.codehaus.plexus:plexus-utils:jar:2.0.5
  Included: org.apache.commons:commons-exec:jar:1.1
  Excluded: org.apache.maven:maven-toolchain:jar:1.0
  Excluded: org.apache.maven:maven-project:jar:2.0.6
  Excluded: org.apache.maven:maven-settings:jar:2.0.6
  Excluded: org.apache.maven:maven-profile:jar:2.0.6
  Excluded: org.apache.maven:maven-plugin-registry:jar:2.0.6
  Excluded: org.apache.maven:maven-model:jar:2.0.6
  Excluded: org.apache.maven:maven-artifact:jar:2.0.6
  Excluded: org.apache.maven:maven-artifact-manager:jar:2.0.6
  Excluded: org.apache.maven:maven-repository-metadata:jar:2.0.6
  Excluded: org.apache.maven:maven-core:jar:2.0.6
  Excluded: org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.6
  Excluded: org.apache.maven:maven-error-diagnostics:jar:2.0.6
  Excluded: org.apache.maven:maven-plugin-descriptor:jar:2.0.6
  Excluded: org.apache.maven:maven-monitor:jar:2.0.6
  Excluded: classworlds:classworlds:jar:1.1
  Excluded: org.apache.maven:maven-plugin-api:jar:2.0.6
  Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9
  Excluded: junit:junit:jar:3.8.2
Configuring mojo org.codehaus.mojo:exec-maven-plugin:1.2.1:exec from plugin realm ClassRealm[plugin>org.codehaus.mojo:exec-maven-plugin:1.2.1, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.2.1:exec' with basic configurator -->
  (f) basedir = C:\Users\Birger\Workspace\myproject
  (f) classpathScope = runtime
  (f) commandlineArgs = -Djava.library.path=lib\ -classpath %classpath com.mysite.myproject.Main
  (f) executable = C:\Program Files\Java\jdk1.8.0_91\bin\java.exe
  (f) longClasspath = false
  (f) project = MavenProject: com.mysite:myproject:1.0-SNAPSHOT @ C:\Users\Birger\Workspace\myproject\pom.xml
  (f) session = org.apache.maven.execution.MavenSession@2ef14fe
  (f) skip = false
-- end configuration --
Collected project artifacts [jni4net:jni4net.j:jar:0.8.8.0:compile]
Collected project classpath [C:\Users\Birger\Workspace\myproject\target\classes]
dealing with jni4net:jni4net.j:jar:0.8.8.0:compile
Toolchains are ignored, 'executable' parameter is set to C:\Program Files\Java\jdk1.8.0_91\bin\java.exe
Executing command line: C:\Program Files\Java\jdk1.8.0_91\bin\java.exe -Djava.library.path=lib\ -classpath C:\Users\Birger\Workspace\myproject\target\classes;C:\Users\Birger\.m2\repository\jni4net\jni4net.j\0.8.8.0\jni4net.j-0.8.8.0.jar com.mysite.myproject.Main
Initialized!
Hello world!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.360s
Finished at: Fri Jul 08 09:26:48 CEST 2016
Final Memory: 5M/245M
------------------------------------------------------------------------

尝试运行我构建的 .jar 时的命令行输出

C:\Users\Birger\Workspace\myproject\target>"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" -Djava.library.path=lib\ -jar myproject-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Birger\Workspace\myproject\target\lib\jni4net.n-0.8.8.0.dll: Can't load this .dll (machine code=0xbd) on a AMD 64-bit platform
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
        at java.lang.Runtime.loadLibrary0(Runtime.java:870)
        at java.lang.System.loadLibrary(System.java:1122)
        at com.mysite.myproject.Main.<clinit>(Main.java:11)

编辑 2:

对于任何想要重现此错误的人,可以找到 jni4net here .我用这个 Windows 批处理文件安装了 .jar:

set project_dir=YOURPROJECTDIRECTORYHERE
set proxygen_dir=YOURPROXYGENINSTALLATIONDIRECTORYHERE
set temp_repo_dir=%project_dir%\temp-repo
call mvn install:install-file -DlocalRepositoryPath=%temp_repo_dir% -DcreateChecksum=true -Dpackaging=jar -Dfile=%proxygen_dir%\lib\jni4net.j-0.8.8.0.jar -DgroupId=jni4net -DartifactId=jni4net.j -Dversion=0.8.8.0

编辑 3:

我安装了 32 位 JVM,并尝试使用以下命令运行应用程序:

"C:\Program Files (x86)\Java\jre1.8.0_91\bin\java.exe" -Djava.library.path=lib\ -jar myproject-1.0-SNAPSHOT.jar

现在我得到:

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Birger\Workspace\myproject\target\lib\jni4net.n.w32.v20-0.8.8.0.dll: Can't load this .dll (machine code=0xbd) on a IA 32-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at com.mysite.myproject.Main.<clinit>(Main.java:19)

我在这里变得非常绝望(并且对 Java tbh 有点沮丧)

编辑4:

试过这些命令,也没有用:

"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Djava.library.path=lib\ -classpath C:\Users\Birger\Workspace\myproject\target\classes;C:\Users\Birger\.m2\repositor‌​y\jni4net\jni4net.j\0.8.8.0\jni4net.j-0.8.8.0.jar com.mysite.myproject.Main
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Djava.library.path=lib\ -classpath classes;C:\Users\Birger\.m2\repositor‌​y\jni4net\jni4net.j\0.8.8.0\jni4net.j-0.8.8.0.jar com.mysite.myproject.Main
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Djava.library.path=lib\ -classpath classes com.mysite.myproject.Main

最佳答案

我很久以前就遇到过这个问题。

只需删除 <filtering>true</filtering> ,这会在复制时损坏文件。 (漏洞 !!???)。

我能够重现您的问题。这也将解决您的问题

希望这对您有所帮助。

关于java - 当我不能时,Maven 正在执行什么样的魔法来运行这个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38260628/

有关java - 当我不能时,Maven 正在执行什么样的魔法来运行这个项目?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  7. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  8. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

  9. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  10. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

随机推荐