How to connect to rexster server over rexpro within the java code? - java

I have a rexster server running locally on my machine on port 8984. I want to connect to my graph dadabase (orientdb) and execute gremlin scripts within my java code. I couldn't find any good example or tutorial on doing that.
Here is my code:
import com.tinkerpop.rexster;
import com.tinkerpop.rexster.*;
public class Orient {
public static void main(String[] args) {
RexsterClient client = RexsterClientFactory.open("localhost", 8984);
String script = String.format("g=rexster.getGraph('%s');g.v('%s').map", "test_test", "9:6267");
List<Map<String, Object>> results = client.execute(script);
Map<String, Object> map = results.get(0);
System.out.println(map.get("name"));
}
}
when I try to compile my code like :
$javac -cp rexster-protocol-2.6.0.jar Orient.java
I get this :
Orient.java:1: error: package com.tinkerpop does not exist
import com.tinkerpop.rexster;
^
Orient.java:2: error: package com.tinkerpop.rexster does not exist
import com.tinkerpop.rexster.*;
^
Orient.java:7: error: cannot find symbol
RexsterClient client = RexsterClientFactory.open("localhost", 8984);
^
symbol: class RexsterClient
location: class Orient
what am I doing wrong? where can I get the dependencies (.jar) files. if any needed.
Thanks

You need to add all dependent jar files in classpath to compile
http://mvnrepository.com/artifact/com.tinkerpop.rexster/rexster-protocol/2.6.0
You would better use maven or gradle build tool rather downloading all the jar files and typing compile command manually.

Related

Could not find or load main class in a simple java project

I have created a simple java project "DesignPatterns".
I created a file FacadePattern.java with the path being ~/DesignPatterns/Structural/FacadePattern/FacadePattern.java
My FacadePattern.java class,
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But my editor (VSCode) gives me an error at the start of the file:
The declared package "" does not match the expected package "Structural.DecoratorPattern"
So upon clicking on quick fix, it added package Structural.FacadePattern; in the first line.
So the final code became
package Structural.FacadePattern;
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But when I compile the above file using the command
javac FacadePattern.java && java FacadePattern
It is giving me the following error:
Error: Could not find or load main class FacadePattern
Caused by: java.lang.NoClassDefFoundError: Structural/FacadePattern/FacadePattern (wrong name: FacadePattern)
After searching a lot in the internet, I ran the following command:
javac -sourcefile ~/DesignPatterns FacadePattern.java && java FacadePattern
But no use, I am getting the same error.
Can anyone explain why my editor gave an error but code ran successfully before? and why wont it compile after adding "package Structural.FacadePattern;" line? and what is -sourcefile parameter in javac command? and how to run the code with successfully without errors in editor as well as the terminal?
Stick to naming rules. Package names should be lowercase only. In your case, it should be package structural.facadepattern;
Run the 'correct' class. Because your class is not inside 'the base folder' aka the 'default package', you have to prefix the class with its package name: java Structural.FacadePattern.FacadePattern or rather java structural.facadepattern.FacadePattern if you use the proper case for packages.

Java can't find class in same package when I run Main from command line

I moved a class from Main.java into their own .java files and now the IDE (IntelliJ) can't find them, even though they are in the same package. Here are the first lines of Main...
package readability;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
var filePath = args[0];
var textAnalyser = new TextAnalyser(filePath);
... and here's the error I get when I run it from the command line:
C:\Users\123md\IdeaProjects\Readability Score\Readability Score\task\src\readability>java Main.java in.txt
Main.java:8: error: cannot find symbol
var textAnalyser = new TextAnalyser(filePath);
^
symbol: class TextAnalyser
location: class Main
Interestingly, when I just say String filePath = "in.txt" and run it in the console, it does find the class and runs fine, so why can't it find the class when I run it from the command line? Thanks!
You can only run single *.java file with java command. If your program uses mutiple files, you have to compile it first:
javac readability/Main.java readability/TextAnalyser.java
and then run:
java readability.Main in.txt
For more info see: https://openjdk.java.net/jeps/330

Java Incompatible Types error after placing classes into a package

I have a package named "shapefile" which contains classes IndexFileReader and IndexFile. IndexFileReader returns an object of type IndexFile.
I then have a class named ReadFiles which imports "shapefile" and uses IndexFileReader to create an IndexFile.
import java.io.IOException;
import shapefile.*;
public class ReadFiles {
public static void main(String[] args) throws IOException{
IndexFile indexFile;
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
}
}
My files are located in folders as follows:
JavaProjects
shapefile
IndexFileReader.java
IndexFile.java
MyProject
ReadFiles.java
I compile the shapefile project from the Windows command line like this and it compiles fine:
cd JavaProjects
javac .\shapefile\*.java
I compile my project like this and I get an error:
cd MyProject
javac -cp .. ReadFiles.java
Error:
ReadFiles.java:8: error: incompatible types
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
^
required: IndexFile
found: shapefile.IndexFile
This is new to me and I believe I am doing something wrong with running javac. I have successfully compiled ReadFiles previously, I thought I did so with the same commands I've printed here. I actually still have the ReadFiles.class file from when this compiled successfully and it runs without any problems, which is why I believe I am not using javac correctly.
Why is Java giving an error after requiring an object of "IndexFile" and receiving an object of "shapefile.IndexFile"? Aren't these objects the same type?
Why is my IndexFileReader class returning an object of type "shapefile.IndexFile" instead of an object of type "IndexFile"?
Is there anything with how I have my files/folders laid out that will cause problems with compiling?
What command to I provide Windows to correctly use javac to compile my package and to compile my ReadFiles class?
I have looked through the other Incompatible Types questions and I have not found one that had led me in the right direction.

Java cannot find symbol when compiling

I am following a Java tutorial (left to my own devices to write the test code), but when trying to compile I get a symbol not found error. I've looked and looked, but cannot work out why the code I have written produces this error. It's probably very simple, but I'd appreciate someone pointing out the cause as I'm pulling my hair out trying to understand what I've done wrong!
TestBeerExpert.java:
package com.example.model;
import com.example.model.*;
import java.util.*;
public class TestBeerExpert {
public static void main(String[] args) {
TestBeerExpert test = new TestBeerExpert();
test.go();
}
private void go() {
BeerExpert expert = new BeerExpert();
List<String> brands = expert.getBrands("amber");
...
}
}
BeerExpert.java:
package com.example.model;
import java.util.*;
public class BeerExpert {
public List<String> getBrands(String color) {
List<String> brands = new ArrayList<String>();
...
return(brands);
}
}
Directory structure:
beerV1 -> src -> com -> example -> model -> TestBeerExpert.java & BeerExpert.java
Compiling from beerV1 with javac -d classes src/com/example/model/TestBeerExpert.java
And the actual error:
src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol
BeerExpert expert = new BeerExpert();
^
symbol: class BeerExpert
location: class TestBeerExpert
src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol
BeerExpert expert = new BeerExpert();
^
symbol: class BeerExpert
location: class TestBeerExpert
2 errors
For the life of me I can't work out what I'm doing wrong. The files are in the same directory and package, so as far as I am aware this should be compiling. I'd be grateful to now only have the code corrected but and explanation of what I have done wrong so I can remember it for the future. Thanks in advance.
Add src to your sourcepath
javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java
You need to do this because you execute javac from a different directory from where the sources are.
No need to compile BeerExpert first (javac will do it for you with the above command).
Validatation:
~/beerV1$ ls src/com/example/model/
BeerExpert.java TestBeerExpert.java
~/beerV1$ ls classes
~/beerV1$ javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java
~/beerV1$ ls classes/com/example/model/
BeerExpert.class TestBeerExpert.class
You are getting the compile error because compiler is not able to find BeerExpert class. Try below
1. first compile BeerExpert.java using :
javac -d classes src\com\example\model\BeerExpert.java
2. then compile TestBeerExpert.java
javac -cp classes -d classes src\com\example\model\TestBeerExpert.java

Different behaviour of netbeans and console

I have written a code in java. In which I have created a package called xml-creator.
Package xml_creator has 3 classes say XML_Control, XML_Creator, and XML_implement.
When I run my project on netbeans (NetBeans 7.0) it works fine. But if I try to compile code on console, I get various errors like
When I compiled XML_Creator.java, I get following errors.
XML_Creator.java:371: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
typeAttr.setValue(XML_implement.table_col[i][2]);
^
XML_Creator.java:375: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
for(int j=0;j<XML_implement.kTab;j++)
^
XML_Creator and XML_implemenr both are in same package but non of them extend each other.
I am sorry I cant show code on this site as it is too large and aginst the company's policies.
I dont understand why it is showing me errors?
Sample code
XML_Control.java
package xml_creator;
public class XML_Control
{
public static void main(String as[])
{
XML_Creator xml = new XML_Creator();
}
}
XML_Creator.java
package xml-creator;
public class XML_Creator
{
XML_implement ixml = new XML_implement();
public XML_Creator()
{
System.out.println(""+ixml.a);
}
}
XML_implement.java
package xml_creator;
public class XML_implement
{
public int a;
public XML_implement()
{
a = 10;
}
}
So when I compile XML_Creator.java, console gives error.
It sounds like you're compiling within the directory containing the .java file, and only telling the compiler about one of the source files. That's the problem - to try to find a source or class file, the compiler is using the package name, and expecting the packages to be laid out in the conventional fashion. Compile from the root of the source tree - which I certainly hope you're using - like this:
javac xml_creator/*.java
You may also want to specify an output directory - which again will be the root of the directory hierarchy for packages:
javac -d bin xml_creator/*.java
If you're building regularly from the command-line (and not just for throwaway code) you should look into using a build system such as Ant.

Categories