Some .java source codes from Android SDK not found - java

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!

Related

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

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.

Javah and Eclipse missing class files (android.support.v4.app.FragmentActivity)

I want to generate headers using javah but when i do i get an error:
Error: cannot access android.support.v4.app.FragmentActivity
class file for android.support.v4.app.FragmentActivity not found
I created new External Tool with this parameters:
Location: D:\Android\jdk1.7.0_51\bin\javah.exe
Working Directory: D:\Android\workspace\MyApp\bin\
Arguments: -d D:\Android\workspace\MyApp\jni -classpath D:\Android\workspace\MyApp\bin\classes;D:\Android\workspace\appcompat_v7\libs\android-support-v7-appcompat.jar com.example.myapp.MainActivity
I use last version of Android SDK and NDK as last version of Java.
Any suggestions?
This can be solved by adding android-support-v4.jar to your javah class path arguments.
I am not an Android developer, this was a result of hunting around on Google (for example, information derived indirectly from the answer to IntelliJ 12 + ActionBarSherlock setttings: Could not find class 'android.support.v4.app.FragmentActivity'). I do not know why this is required in addition to standard v7 jars, and I am posting this answer as community wiki in hopes that somebody with more experience can edit it to add some more information in the future.

Debugging java file compiling (using gwt)

I was asked in a precedent question to be more precise about my compilation error message. Here's the fact : I know nothing about GWT and Java. Following the docs, I tried to compile Java files from a web project that had been precedently developed using GWT. So, to test and understand how all this work, I took the java folder (that had been precedently compiled with an appropriate tool)
into src folder of a web app project in my ide Eclipse
When running the compiler using the command GWT Compile, I had this message error :
Compiling module java.org.primagora
Validating newly compiled units
Ignored 5 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Finding entry point classes
[ERROR] Unable to find type 'org.client.primagoraEntryPoint'
[ERROR] Hint: Check that the type name 'org.client.primagoraEntryPoint' is really what you meant
[ERROR] Hint: Check that your classpath includes all required source roots
When I look at the error on the file, for example java.org.client.primagoraEntryPoint, I find an error when it is declared "package org.client" at the very beginning of the file. There seem to be an error path. I thought the java folder I took would be correctly implemented in Eclipse.
Is that clearly a file path problem ? How should I debugg it ? (I reallly know nothing about gwt, java , eclipse)
Best,
Newben
Where is your GWT module file (i.e. a file that ends in .gwt.xml), and what are its contents? The package you list for your entrypoint is org.client, and the full name is org.client.primagoraEntryPoint, suggesting that there is a file in org/client/primagoraEntryPoint.java. By default, module files include the client package relative to them as source, so if the file is in the wrong location, this won't work correctly.
Based on this, your module file should be in the org package:
src/
org/
SampleModule.gwt.xml
client/
primagoraEntryPoint.java
From the very beginning of your error, you list java.org.primagora as the module:
Compiling module java.org.primagora
This suggests the following structure:
src/
org/
client/
primagoraEntryPoint.java
java/
org/
primagora.gwt.xml
This doesn't make sense, since GWT is now looking for a java.org.client package instead of a org.client package. Try using the package setup mentioned earlier here.
A better option might be to pick an existing working project, like what the GWT plugin for Eclipse will create, or one of the samples in the GWT download.

Java Will Not Compile Objects Created by Me

I have recently been getting this Java compile error every time I try to compile code that creates an instance of a class that I have created. I've tried compiling manually, compiling from a different location, and even tried compiling in safe mode. I have also completely reinstalled Java on my computer. Here's an example of code I write and the error I always get:
Instance creator class:
public class Nothing {
public static void main(String args[]) {
Can World = new Can();
}
}
Instantiated class:
public class Can {
public Can() {
System.out.println("Test");
}
}
The compile error:
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
2 errors
Someone who knows Java better than me has tried to compile files that I have had problems with with no success. Also, when I run the code from within Eclipse, my IDE, it runs like it should.
Any suggestions at all or solutions would really, really be appreciated. I would really hate something like this to be the thing that prevents me from programming. Thanks again.
EDIT: I used to be able to compile the classes like this, until only recently I started receiving this error. I am compiling using an external tool I have created in the Eclipse IDE, but I have also tried compiling simply by navigating to the directory the two files are in in a CMD window and used javac Nothing.java yet the same error arises. I have also tried compiling Can.java first (which compiles), and then Nothing.java, but this fails as well. Here is the text that I get when compiling from a CMD window:
02/09/2011 06:44 PM <DIR> .
02/09/2011 06:44 PM <DIR> ..
02/09/2011 03:45 PM 289 .classpath
02/09/2011 03:45 PM 382 .project
02/09/2011 03:45 PM <DIR> .settings
02/09/2011 06:00 PM 75 Can.java
02/09/2011 05:49 PM 102 Nothing.java
4 File(s) 848 bytes
3 Dir(s) 64,669,216,768 bytes free
C:\Users\Alex\Mindstorms\NXT\leJOS NXJ\Moment>javac Nothing.java
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
2 errors
The problem is that you need to have an appropriate import statement (if in separate packages), and you need to run the Java compiler from the appropriate directory. Supposing your directory structure looks like:
src/
com/
yourdomain/
example/
Can.java
Nothing.java
Then you would need the following at the top of both your *.java files:
package com.yourdomain.example;
And you should put the following import statement in Nothing.java (technically this is not necessary when they are both in the same package, but is necessary when in separate packages, and it's a good habit):
import com.yourdomain.example.Can;
And then you would invoke the compiler from within the src directory as follows:
javac com/yourdomain/example/*.java
And you could then run this program using:
java com.yourdomain.example.Nothing
By the way, you really shoudn't be building projects by hand this way; you should use an automatic build system such as Maven or Ant. If you create your project using the NetBeans IDE, aside from giving you simple "Build", "Run", and "Build & Run" buttons plus all sorts of nice IDE features (code highlighting, incremental compilation and suggestions for fixes), it will generate an Ant build project for you.
Edit
Based on your updated question,... note that Eclipse's compiler is distinct from javac. If you installed javac using Cygwin or if you've been sharing your files between Windows and UNIX (possibly through a version control system), you may have run into an encoding issue. I would suggest resaving those files in UTF-8 and running unix2dos (or vice-versa if you installed javac via Cygwin) and recompiling. If that doesn't work, it might be worth reinstalling javac. Failing that, there's always Ubuntu ;).
By putting the code you've presented for each class into separate files (named Can.java and Nothing.java, of course) and having both files in the same directory they compile for me using the command line compiler:
javac Nothing.java
The error indicates that the compiler can't find the Can class when compiling Nothing. Do you have both files in the same directory?
I have figured it out. As it turns out, I had tried setting up an older version of Lego Robotics with Java on my computer, and I had created some environment variables to go with it. One of them was the CLASSPATH variable. I am pretty confident that Java was trying to read that variable (which I was no longer using) to look for my classes. Once I deleted it (and the other old variables that I wasn't using), restarted my computer, and reinstalled the JDK, everything works.
Thanks for the help from everybody though :)

Categories

Resources