Protocol Buffer: How to import? - java

I have 2 .proto files :
First file:
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
Second file:
package com.test.model;
message CustomObj {
required string smth=1;
}
The issue here is that "CustomObj" is said to be "unresolved reference" .
Thus, I've tried to import the second file into first file:
import "com/test/model/firstFile.proto"
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
I still get the same issue !!

The import statement is the folder relative to the place where you invoke protoc.
It looks like you have treated it as relative to the package instead.
e.g. if (like me) you store both files in src/main/resources, you'd invoke protoc as follows:
protoc src/main/resources/firstFile.proto src/main/resources/secondFile.proto --java_out=src/generated/java
and your import statement would be import "src/main/resources/firstFile.proto"
If you want to store the files in subfolders according to package name, then you just add this accordingly, after the top-level foldername.
HTH

Related

Unable to locate correct java.util

I am trying to compile a small program. During compilation, it tries to look at wrong path for java util libs. Below is the compilation output
javac -Xlint:unchecked adssim/util/XMLParser.java
adssim/util/XMLParser.java:3: error: cannot access util
import java.util.Vector;
^
bad source file: ./java/util.java
file does not contain class java.util
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
File loation
/apps/oasis/adarsh/adssim/util>ls -lrt XMLParser.java
-rwxrwxrwx 1 oasis oasis 1258 Apr 8 15:49 XMLParser.java
Classpath
/apps/oasis/adarsh/adssim/util>echo $CLASSPATH
:/apps/oasis/java/classes12.jar:/apps/oasis/java/ifxjdbc.jar:/apps/oasis/java/log4j-1.2.14.jar:/apps/oasis/java/commons-io-2.5.jar:/apps/oasis/adarsh/adssim:/apps/oasis/adarsh/adssim/util:/apps/oasis/adarsh/adssim/util/crypto:/apps/oasis/adarsh/adssim/channel:/apps/oasis/adarsh
File Source
package adssim.util;
import java.util.Vector;
public class XMLParser
{
public static Vector getXMLTagValue(String xml, String section) throws Exception
{
String xmlString = new String(xml);
Vector v = new Vector();
What am i doing wrong guys ?
Is there a file on your disk that is actually called util.java in a directory called java? Maybe you have created such a file by mistake?
This part of the error message makes it sound like that:
bad source file: ./java/util.java

How to move one folder back in java while importing packages

I have one Folder by the name of "java" which has two folders "Search","Demo".
in "Search" folder I have so many class including "BinarySearch.java" and in "Demo" folder I have class "Demo.java" I want to import "BinarySearch.java" class in the "Demo.java" class.
So I tried this:
First in "BinarySearch.java" I put this line of code package Search;
in "Demo.java" I put this line of code import Search.BinarySearch.java
But this solution only works only when I move the "Demo.java" file in "java" folder. but I need to import "BinarySearch.java" without moving my file into "java" folder.
See the error If don't move the file into "java" folder
So I need to move back one folder while importing.
Use this:
In Demo/Demo.java
package Demo;
import Search.BinarySearch;
public class Demo {
.
.
.
}
In Search/BinarySearch.java
package Search;
public class BinarySearch {
.
.
.
}
It is always a good practice to use lower case for your package names. Example: demo or search.
The first line of Demo/Demo.java must be "package Demo".
The same as the first line of Search/BinarySearch.java is "package Search".

Eclipse java file not found until edited and saved

I have the following file structure in my eclipse workspace
core
src
com.mygame.game (package)
Ability.java
Game.java
...
Allies-Green.java
Ability.java and Game.java were created using the 'right-click on package->New->Class' method. Allies-Green.java, however, was created using the 'right-click on package->New->File' method. Each time I load my workspace, all code that references Allies-Green.java is marked as an error. If I open Allies-Green.java though, and edit one character and resave it, the problem goes away. Does anyone know what could be causing this issue? Here is Allies-Green.java relevant code for reference:
package com.mygame.game;
import java.util.ArrayList;
import ...
//only class in file
class AllyGreen extends Ally {
...
}

How can I add java client library in java on netbeans?

Hello i read that I have to use the java client library in java to get the revisions list in google drive using google api. I use netbeans. I search for this question and I try to add the bib but i haven't get success. To download the library i visited this link and downloaded the "latest". but after add the jar files I get the message error that the package doesnt exist. Please someone help me!
Yes! I try this but it show the same erros. I add the lib but netbeans (8) show the error in the three lines code: "package [name] doesn't exist".
The three lines are these:
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
obs: the full code, taken in the google developers site is:
package javaapplication8;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
import java.io.IOException;
import java.util.List;
public class MyClass {
private static List<Revision> retrieveRevisions(Drive service,
String fileId) {
fileId = "1XpNdeTFBr2KygyfPtlowBvkpcaJJzjgLckrGjp5oOhg0";
try {
RevisionList revisions = service.revisions().list(fileId).execute();
return revisions.getItems();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
}
If it is the error like package is missing, please check whether you have created a folder named com in your project or not. Also, every dot (.) indicates the levels of packages that you use in your project. Please check that com folder followed by the other folder names after every dot are present or not. I think this might help you. I answered your question based on my understanding (i.e, package issue which will be resolved by creating folders in your project. folders means packages). I am sorry if I gave you the wrong answer.

How to use .jar in a pig file

I have two input files smt.txt and smo.txt. The jar file reads the text files and split the data according to some rule which is described in java file. And the pig file takes these data put into output files with doing mapreduce.
register 'maprfs:///user/username/fl.jar';
DEFINE FixedLoader fl();
mt = load 'maprfs:///user/username/smt.txt' using FixedLoader('-30','30-33',...........) AS (.........);
mo = load 'maprfs:///user/username/smo.txt*' using FixedLoader('-30','30-33',.....) AS (......);
store mt into 'maprfs:///user/username/mt_out' using JsonStorage();
store mo into 'maprfs:///user/username/mo_out' using JsonStorage();
and a part of java code like in the following. (The content of methods are not neccessary I believe):
package com.mapr.util;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.io.*;
import org.apache.pig.*;
import org.apache.pig.data.*;
import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.*;
import java.util.*;
import java.io.*;
public class FixedLoader extends LoadFunc
{
............
}
When I run this pig program in a teminal with the command "pig -x mapreduce sample.pig", I gave an Error message:
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1070: Could not resolve com.mapr.util.FixedLoader using imports: [, org.apache.pig.builtin., org.apache.pig.impl.builtin.]
How can I import these into my project or are there any suggestions/solutions to run this program?
You need to define FixedLoader with its full package name:
register 'maprfs:///user/username/fl.jar';
DEFINE FixedLoader com.mapr.util.FixedLoader();
...
Also register all of the 3rd party dependency jars that are used in your custom UDF.

Categories

Resources