我有以下代码
package myPackage;
import org.neo4j.graphdb;
import org.neo4j.kernel.EmbeddedGraphDatabase;
public class dbServlet extends HttpServlet {
public void init() throws ServletException {
// Start up the database here
GraphDatabaseService graphDb = new EmbeddedGraphDatabase("var/base");
}
public void destroy() {
graphDb.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
和 build.xml 文件:
<project name="dbServlet" basedir="." default="compile">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>
</project> …Run Code Online (Sandbox Code Playgroud) 我已经有很长一段时间了,因为我已经广泛使用了java,而且我遇到了一些麻烦,我认为这可能很简单.代码在linux系统上,我正在使用javac和其他命令行工具.
两个文件,第二个不会编译.这是第一个名为ITranslator.java的文件:
package org.helloopensource.greetings;
public interface ITranslator {
public abstract String translate(String fromLanguage, String toLanguage, String word);
}
这是第二个,名为Greeting.java:
package org.helloopensource.greetings;
public class Greeting {
private ITranslator translator;
public Greeting(ITranslator translator) {
this.translator = translator;
}
public String sayHello(String language, String name) {
return translator.translate("English", language, "Hello") + " " + name;
}
}
当我尝试编译时,我得到:
> javac -classpath `pwd` Greeting.java
Greeting.java:4: cannot find symbol
symbol : class ITranslator
location: class org.helloopensource.greetings.Greeting
private ITranslator translator;
^
Greeting.java:6: cannot find symbol
symbol : … 我在Windows XP下使用Cygwin,我正在尝试让Ant使用JUnit而不编写任何类路径条目.JUnit的文档说我可以:
将junit.jar和ant-junit.jar放在ANT_HOME/lib中.
我在JUnit in Action中的便利副本说:
您可能已经注意到,您没有将JUnit JAR显式添加到类路径中.请记住,在安装Ant时,您将JUnit JAR文件复制到$ {ANT_HOME}/lib目录以使用junit Ant任务.因为junit.jar已经在您的类路径中,所以您无需在javac任务中指定它来编译测试.
$ ANT_HOME设置为Ant安装目录.在lib子目录中,我看到ant-junit.jar(与Ant一起安装)和junit-4.9.jar(由我复制).好像没事吧?但是测试编译步骤失败:
[javac] Compiling 1 source file to E:\tools\ctg\bin\test
[javac] E:\tools\ctg\src\test\TestAll.java:3: package org.junit does not exist
[javac] import org.junit.*;
Run Code Online (Sandbox Code Playgroud)
等等.我尝试将junit-4.9.jar重命名为junit.jar.没有快乐.我偷看了jar文件,看到包里面确实存在包.实际上,如果我在Ant类路径中明确包含那个jar文件,那么编译工作正常.
的Bleh.什么可能是错的?谢谢.
编辑:根据要求,这是javac目标:
<path id="classpath">
<pathelement location="${app-bin}"/>
<pathelement location="${test-bin}"/>
<!--pathelement location="${junit-bin}"/-->
</path>
<target name="compile-test" depends="compile-app">
<javac srcdir="${test-src}"
destdir="${test-bin}"
source="${java}"
target="${java}"
debug="on"
includeantruntime="false">
<compilerarg value="-Xlint"/>
<classpath refid="classpath"/>
</javac>
</target>
Run Code Online (Sandbox Code Playgroud)
请注意,JUnit二进制文件已注释掉.我的目标是删除它并使编译工作.
我不确定为什么这段代码(在运行时给出stackoverflow)编译:
import java.io.*;
import java.util.*;
public class StackOverflow {
StackOverflow overflow = new StackOverflow();
public void myCall() {
overflow.myPrint();
}
public static void main(String[] args) {
StackOverflow newStackOverflow = new StackOverflow();
newStackOverflow.myCall();
}
public void myPrint() {
System.out.println("I am confused!");
}
}
Run Code Online (Sandbox Code Playgroud)
我之所以感到困惑的原因是因为在类定义中,我正在尝试创建我想要定义的类的对象.这不应该是编译时错误吗?
我想使用某种编辑器打开由Java Compiler生成的字节代码(Java二进制文件),这允许我查看字节代码(Raw,但人类可以理解,采用此处定义的格式)并直接修改.class文件中的内容.
我试过这个javap实用程序.但通过javap,我无法更改我的.class文件,它似乎也没有显示原始字节代码
此外,我已经看到像JD Decompiler这样的反编译器,它给了我来自.classFile 的代码.但我对源代码不感兴趣,我想查看字节码.
另外,我尝试过名为dirtyJOE&JBE的 GUI编辑器.这个编辑器非常好,完成了我的目的的一半.我可以在.class文件中看到各种字段并编辑我的.class文件.但这似乎也就像一个翻译器,它解码字节码并在UI上显示它以便于理解.它没有显示原始字节代码.
甚至可以转到原始字节码?
我可能听起来很愚蠢,但我想确认这一点,以正确理解Java编译器和JVM.
我尝试在编译java源时添加-bootclasspath选项,如下所示:
javac -classpath lib/* -target 1.6 -source 1.6 -bootclasspath /usr/lib/jvm/java-7-oracle/lib/*.jar Hello.java
Run Code Online (Sandbox Code Playgroud)
编译时出现以下错误:
javac: invalid flag: /usr/lib/jvm/java-7-oracle/lib/dt.jar
Usage: javac <options> <source files>
use -help for a list of possible options
Run Code Online (Sandbox Code Playgroud)
我应该如何添加bootclasspath参数?
我需要构建android 4.3 master.按照http://source.android.com/source/building.html上的说明操作.它很好地从repo同步源代码.当我们尝试使用"make -j4"构建时,这种情况正在发生.
1 error
make: *** [out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes-full-debug.jar] Error 41
make: *** Waiting for unfinished jobs....
Run Code Online (Sandbox Code Playgroud)
这是完整的日志 -
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.3.2.1.000.000
TARGET_PRODUCT=aosp_panda
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a9
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-2.6.32-38-generic-x86_64-with-Ubuntu-10.04-lucid
HOST_BUILD_TYPE=release
BUILD_ID=OPENMASTER
OUT_DIR=out
============================================
No private recovery resources for TARGET_DEVICE panda
target Java: core (out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes)
host Executable: aidl (out/host/linux-x86/obj/EXECUTABLES/aidl_intermediates/aidl)
host C++: aapt <= frameworks/base/tools/aapt/AaptAssets.cpp
host C++: aapt <= frameworks/base/tools/aapt/Command.cpp
host C++: aapt <= frameworks/base/tools/aapt/CrunchCache.cpp
host C++: aapt <= frameworks/base/tools/aapt/FileFinder.cpp
host C++: aapt <= frameworks/base/tools/aapt/Main.cpp
host C++: …Run Code Online (Sandbox Code Playgroud) 如果在文件名错误的文件中定义了公共类,javac则会抛出错误:
square_supplies.java:5: error: class Answer is public, should be declared in a file named Answer.java
Run Code Online (Sandbox Code Playgroud)
是否有一个标志可以关闭此行为?如果它不可能javac,是否有另一个java编译器可以关闭它?
你可以从标题中收集,这是一个有点复杂的问题.
首先,我的目标:
我试图实现我的Java类与JSON之间的转换,而不必向它们添加任何特定于json的注释.
我的java类包括immutables,它必须从传递给构造函数的参数初始化它们的成员,所以我必须有多参数构造函数,它们在没有@JsonCreator且没有@JsonParameter的情况下工作.
我正在使用jackson ObjectMapper.如果我可以使用的另一个ObjectMapper没有这里描述的问题,我会很乐意使用它,但它必须与jackson ObjectMapper 一样有信誉.(所以,我不愿意从他的GitHub下载Jim的ObjectMapper.)
我理解如何实现这一点,以防万一我错了:
Java用于使方法(和构造函数)参数类型可通过反射发现,但不能通过参数名称发现.这就是为什么@JsonCreator和@JsonParameter注释过去是必要的:告诉json ObjectMapper哪个构造函数参数对应于哪个属性.使用Java 8,如果提供新-parameters参数,编译器会将方法(和构造函数)参数名称发送到字节码中,并通过反射使它们可用,并且最近版本的jackson ObjectMapper支持这一点,因此现在应该可以拥有json对象映射,没有任何特定于json的注释.
我有这个pom.xml:
<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>test</groupId>
<artifactId>test.json</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Json Test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<sourceDirectory>main</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<!--<groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!--<compilerArgument>-parameters</compilerArgument>-->
<!--<fork>true</fork>-->
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
我用它来编译和运行以下小的自包含程序: …
因为我们知道,如果我们要创建object的class,我们需要做的是:
class MyClass{
// ...
}
Run Code Online (Sandbox Code Playgroud)
并创建它object我们做:
MyClass MyObj = new MyClass();
Run Code Online (Sandbox Code Playgroud)
现在,如果我们想在java中创建一个程序,我们需要在其中有一个main方法,class并且该方法class应该与文件名相同.
例如:
// file MyFile.java
public class MyFile{
public static void main(String[] args){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当该程序运行时,是否MyFile也会在内部创建类的对象,java以便通过调用main方法来启动java文件的执行,或者它是否main静态调用该方法:
MyFile.main(String[] args);
Run Code Online (Sandbox Code Playgroud)
如果我在课堂上有班级,那会是什么情况MyFile:
public class MyFile{
class HelloWorld{
// ...
}
public static void main(String[] args){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
或者具有非静态属性和方法:
public class MyFile{
int x = 10; …Run Code Online (Sandbox Code Playgroud)