Using guava with j2objc - java

I'm trying to use guava with j2objc but I'm getting the error:
j2objc TestJava.java
translating TestJava.java
error: TestJava.java:1: The import com.google.common cannot be resolved
error: TestJava.java:10: Lists cannot be resolved
Translated 0 files: 2 errors, 0 warnings
while running:
j2objc Test.java
where Test.java contains:
import com.google.common.collect.Lists;
import java.util.ArrayList;
public class Test {
public static void TestMethod() {
ArrayList<String> objects = Lists.newArrayList();
objects.add(0, "Hello world");
System.out.println(objects);
}
}
I've downloaded the lastest release 0.9.5 and added it to .profile:
export PATH=$HOME/bin/j2objc-0.9.5:$PATH
What else do I need to do in order to use guava?
Thanks!

The j2objc translator uses a Java compiler as its front-end, so the same classpath and sourcepath needs to be used when translating as when compiling to Java classes. Here you'll get a similar error if you ran "javac Test.java", because the guava jar needs to be included in the classpath. The j2objc distribution includes lib/guava-jdk5.jar, so run "javac -classpath /lib/guava-jdk5.jar Test.java", and fix any problems with the directory for the guava jar if necessary. Once javac can compile it, substitute "j2objc" for "javac" using the same arguments, and it should translate fine.
You don't always have to use javac first, but whenever there are translation errors reported, it's a quick test to see whether the issue is related to a missing source or class path.
One difference from Java is that when you link the application, the -lguava flag is needed to include that library.

Related

Java file compile using javac unable to refer to other files in the same directory

I am trying to migrate from IDEs like Eclipse to a standalone Java environment, but I'm having problems tying together multiple files into a project.
Here is some sample code, where both files are in the same directory:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
Example.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java in an IDE, the expected output of It's working is printed, however after executing javac *.java the files seem to ignore eachother.
Here is the error that occurs when executing java App.java after it's been compiled:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
How can I compile the files in a project so that they recognise eachother?
If you running Java 11 and above, java App.java will compile App.java only.
If you need to refer Example.java, first you need to compile both java files into a directory.
Let give it named 'classes'. The command will be
javac -d classes *.java
After that, you can run it via
java -cp classes com.example.main.App. Please note that App is without .class suffix
Of course, it is advisable to use build tools like Apache Maven or Gradle to build your project if it grow larger or need other dependencies.

Compilation with javac fails, with eclipse-compiler it works

I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed

Compiling Java with multiple classes with javac

Until now I always used an IDE to write my java apps. Now for different reasons, mainly also to understand more about the language and the things the IDE is just making for you, I switched to vim and have now the following problem:
I wrote two classes:
package seedInformatik.LinearList;
import seedInformatik.LinearList.*;
// A linear list. Contains LinearListElement<T>
public class LinearList<T> {
and
package seedInformatik.LinearList;
public class ListElement<T> {
Now I want to compile LinearList and get the following:
➜ LinearList javac LinearList.java
LinearList.java:10: error: cannot find symbol
private ListElement<T> first; // head of list
^
Both classes are in the same dir. What do I miss?
Many Thanks
Robin
Maybe this can help:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If your files are under some package you need to specify the package like this:
javac com.mypackage/.*java
java com.mypackage.MyClass
I would also recommend using a build tool like maven: http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project

JUnit - Java project structure

I have the following directory structure:
project/hamcrest-core-1.3.jar
project/junit-4.12.jar
project/build/
project/ija/ija2016/HomeWork2Test.java
project/ija/ija2016/homework2/model/cards/Card.java
project/ija/ija2016/homework2/model/cards/CardDeck.java
project/ija/ija2016/homework2/model/cards/CardStack.java
project/ija/ija2016/homework2/model/board/AbstractFactorySolitaire.java
project/ija/ija2016/homework2/model/cards/FactoryKlondike.java
The HomeWork2Test.java was given to us (I cannot edit this one) and contains tests for the other classes. In the header it has these imports:
package ija.ija2016.homework2;
import ija.ija2016.homework2.model.board.AbstractFactorySolitaire;
import ija.ija2016.homework2.model.board.FactoryKlondike;
import ija.ija2016.homework2.model.cards.Card;
import ija.ija2016.homework2.model.cards.CardDeck;
import ija.ija2016.homework2.model.cards.CardStack;
So I made the Card, CardStack and CardDeck classes into a package by specifying:
package ija.ija2016.homework2.model.cards;
in each of the files.
And the AbstractFactorySolitaire and FactoryKlondike have:
package ija.ija2016.homework2.model.board;
Now we are supposed to run the tests in the HomeWork2Test.java class using JUnit. However, when I try to run the following command from the project folder:
javac -cp junit-4.12.jar -d build ija/ija2016/homework2/HomeWork2Test.java
I get errors telling me that the:
package.ija2016.homework2.model.cards does not exist
package.ija2016.homework2.model.board does not exist
I don't exactly understand how to fix the project structure. Also how do I run the JUnit test?
Thank you for replies.
Well I think you are mixing concepts (compiling and running junit tests)
1st you need to compile your Classes (let's compile them to build
dir):javac -d build ija/ija2016/homework2/model/cards/*.java ija/ija2016/homework2/model/board/*.java
2nd you need to compile your test class (you will need to add to classpath what you just compiled and the junit.jar dependency): javac -d build -cp build;junit-4.12.jar ija/ija2016/HomeWork2Test.java
Now you can run your test class (in order to run you need to add to the classpath the build dir and the jar dependencies): java -cp build;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore ija.ija2016.HomeWork2Test
If you want to know more check JUnit 4 Doc
if you are using Linux or MacOS use ":" instead ";" between dirs in classpath

How to get JUnit 4.11 to recognize class in default package?

Similar to other questions on this subject, I am having problems getting JUnit 4.11 to recognize my class.
Similar to the question found here, I am using algs4.jar to write a class, Percolation, which is all in the default package. Just as in the post I linked, I too had difficulty importing algs4 into my class to use the required WeightedWuickUnionUF class found within. I was finally able to compile the program with the following setup.
> echo $CLASSPATH
/Users/dj/Library/Java/Extensions/algs4.jar:
/Users/dj/Library/Java/Extensions/hamcrest-core-1.3.jar:
/Users/dj/Library/Java/Extensions/junit-4.11.jar:
/Users/dj/Library/Java/Extensions/stdlib.jar
Percolation.java
import java.util.Arrays;
import algs4.*;
public class Percolation {
private WeightedQuickUnionUF union_find_ds;
...
}
test_Percolation.java
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.Assert.*;
#RunWith(JUnit4.class)
public class test_Percolation{
private static Percolation perc;
#BeforeClass
public static void testSetup(){
perc = new Percolation(5);
}
#AfterClass
public static void testCleanup(){
perc = null;
}
#Test(expected = IndexOutOfBoundsException.class)
public void testIndexErrorThrown(){
perc.open(0,5);
}
}
Compilation was accomplished with the following commands. Both .java files are in the same folder, so too is a directory titled algs4.jar with the algs4.jar fully expanded such that each class exists as its own file. Therefore in the working directory there are the following files.
Percolation.java
test_Percolation.java
algs4/WeightedQuickUnionUF.class
I feel that this is an important point because even though algs4.jar is within the classpath, the Percolation.java file ONLY compiles when I have algs4/WeightedQuickUnionUF.class in the current working directory. For some reason I cannot get the import to work with the .jar in the classpath.
>javac Percolation.java
>javac test_Percolation.java
Please note that in Percolation.java if I changed the import line from import algs4.*; to import algs4.WeightedQuickUnionUF; I receive the following error at compile time.
Percolation.java:23: error: cannot access WeightedQuickUnionUF
import algs4.WeightedQuickUnionUF;
^
bad class file: ./algs4/WeightedQuickUnionUF.class
class file contains wrong class: WeightedQuickUnionUF
Please remove or make sure it appears in the correct subdirectory of the classpath.
Having compiled both files successfully, I attempted to use the solutions found from the following question. Entering the command java org.junit.runner.JUnitCore test_Percolation or the more verbose but seemingly unecessary command java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore test_Percolation both yield the following error.
JUnit version 4.11
Could not find class: test_Percolation
Time: 0.002
OK (0 tests)
I suspect that there is something wrong with my system configuration on top of the effects of my limited knowledge of Java and importing using Java. I know these questions have been posted elsewhere, but the solutions to said questions have not worked on my machine. Perhaps I have missed a nuance of the answers to other questions.
I am using Mac OSX 10.8.5 doing all of the programming and compilling from the command line using javac 1.7.0_07.
Look at your command-line:
java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
You've got the JUnit jar file on your class path - but not the current directory. So it won't be able to find any classes that aren't either "system" classes or in JUnit.
Try something like:
java -cp .:/Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
Ideally you should also start using packages though - and using more conventional class names. (test_Percolation violates normal conventions.)

Categories

Resources