Compilation with javac fails, with eclipse-compiler it works - java

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

Related

Javac complains that can not see jre library classes during compilation

I am trying to compile java classes using javac. The project uses ByteInputStream class that is located in java_version/jre/lib/rt.jar file(in package com.sun.xml.internal.messaging.saaj.util). javac compilter complains that can't find that class. I tried to include all library classes on during compilation(lib1:lib2:lib3:etc) but still no result.
Here is error message:
package com.sun.xml.internal.messaging.saaj.util does not exist
cannot find symbol : class ByteInputStream
Any ideas?

Some .java source codes from Android SDK not found

I'm new in Android SDK. I'm using Emacs as IDE (I know about Android Studio). I created a project and I compiled it from a script named gradlew (It was created automatically when the project was created). APK is created successfully. Now, I'm trying to implement (with FlyMake) syntax error checker. The command used to do this is the following:
javac "main.java"
where main.java is the main of the application (there is only one file).
Obviously, javac doesn't know where is the SDK (API level 20). So I tell it as follow:
javac -sourcepath "~/opt/android/sources/android-20" "main.java"
but it throws a lot of errors like "class not found". For example:
/home/carlos/opt/android/sources/android-20/android/app/Activity.java:29: error: cannot find symbol
import android.content.IIntentSender;
^
symbol: class IIntentSender
location: package android.content
When I see the content of android/content, there is not a .java named IIntentSender
So, what's happening? Thanks for reading and answering!
This question was answered by CommonsWare:
The last time I used javac by hand, I used -classpath, not -sourcepath. The appropriate JAR for -classpath would be ~/opt/android/platforms/android-20/android.jar, assuming that ~/opt/android/ is where your Android SDK is installed. IIntentSender is generated Java source code (from AIDL) and will not be in Java form in the SDK sources directory. So, if FlyMake requires -sourcepath, I expect that you're going to be in a world of hurt.
Thanks!

How does the terminal compile java programs when $CLASS_PATH is not set? [duplicate]

I'm trying to look under the hood about java compilation. So I put my IDE away and started using MS-DOS command-line...
I created a simple project, as described in the tree below :
SampleApp
|____**src**
|_____pack
|______Sample.java
|____**classes**
This is the Sample.java source code :
public class Sample
{
private String s = new String("Hello, world");
public Sample(){
System.out.println(s);
}
}
I just want to compile this class, so I used the javac command :
prompt\SampleApp\src>javac -d ..\classes -sourcepath . pack\Sample.java
All works fine, but i didn't expect that because I deleted my CLASSPATH environment variable before compiling my Sample.java file. So I was expecting a compiler error due to the fact that the compiler would not be able to find the java.lang.String class file.
I read this article http://www.ibm.com/developerworks/java/library/j-classpath-windows/ which helped me understand many things. The article author says that the default classpath is the current working directory. But I don't understand why my source code compile without error. Could someone explain this to me?
So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.
The short answer is that the compiler knows where to find all of the standard Java SE library classes without you telling it.
The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.
The classpath variable doesn't do what you think. To cite the oracle documentation:
The CLASSPATH variable is one way to tell applications, including the
JDK tools, where to look for user classes. (Classes that are part of
the JRE, JDK platform, and extensions should be defined through other
means, such as the bootstrap class path or the extensions directory.)
Source: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
Basically since java.lang.* is part of the platform and delivered with the JDK/JRE, the compiler doesn't have to be told by you where to look for them.

Using guava with j2objc

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.

Classpath compiles correctly, but will not run. What am I missing

Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
My guess is that . is not on your classpath. For example, you might be compiling with:
javac -cp foo.jar:bar.jar Test.java
but then to run the code you'd need
java -cp foo.jar:bar.jar:. Test
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.
Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.

Categories

Resources