gpt4 book ai didi

java - 资源规范不允许错误

转载 作者:行者123 更新时间:2023-12-01 08:08:37 25 4
gpt4 key购买 nike

在 Eclipse 中运行此 Java 代码时出现以下错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problems:    Resource specification not allowed here for source level below 1.7  The type Transaction is not visible     tx cannot be resolved

at neo4jTesting.EmbeddedNeo4j.createDb(EmbeddedNeo4j.java:52) at neo4jTesting.EmbeddedNeo4j.main(EmbeddedNeo4j.java:38)

这是代码

import java.io.File; import java.io.IOException; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; import org.neo4j.kernel.impl.util.FileUtils;

public class EmbeddedNeo4j {
private static final String DB_PATH = "target/neo4j-hello-db";

public String greeting;

// START SNIPPET: vars
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// END SNIPPET: vars

// START SNIPPET: createReltype
private static enum RelTypes implements RelationshipType
{
KNOWS
}
// END SNIPPET: createReltype

public static void main( final String[] args )
{
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
hello.removeData();
hello.shutDown();
}

void createDb()
{
clearDb();
// START SNIPPET: startDb
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
registerShutdownHook( graphDb );
// END SNIPPET: startDb

// START SNIPPET: transaction
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
// END SNIPPET: transaction
// START SNIPPET: addData
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" );

relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
// END SNIPPET: addData

// START SNIPPET: readData
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// END SNIPPET: readData

greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) );

// START SNIPPET: transaction
tx.success();
} finally {
tx.finish();
}
// END SNIPPET: transaction
}

private void clearDb()
{
try
{
FileUtils.deleteRecursively( new File( DB_PATH ) );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}

void removeData()
{
try ( Transaction tx = graphDb.beginTx() )
{
// START SNIPPET: removingData
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// END SNIPPET: removingData

tx.success();
} finally {
tx.finish();
}
}

void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// START SNIPPET: shutdownServer
graphDb.shutdown();
// END SNIPPET: shutdownServer
}

// START SNIPPET: shutdownHook
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
// END SNIPPET: shutdownHook }

代码来源来自 https://github.com/neo4j/neo4j/blob/master/community/embedded-examples/src/main/java/org/neo4j/examples/EmbeddedNeo4j.java

我对代码墙表示歉意,但我不知道如何用更简单的解决方案来演示该问题。

如有任何建议,我们将不胜感激。

<小时/>

编辑 1

谢谢,我已将 Java 更新到 1.7,但遇到了新错误。这是来自 Eclipse,而不是编译器。

The resource type Transaction does not implement java.lang.AutoCloseable

再次感谢您的帮助。

最佳答案

您正在使用 try-with-resources 语法 (try ( Transaction tx = graphDb.beginTx() )),但您正在使用源进行编译等级1.6。如果可能的话,将源代码合规性更新到 1.7(Java 6 现已 EOL,Java 8 已推出公开测试版),或使用旧式 try-catch-finally block 。

关于java - 资源规范不允许错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com