Friday, November 16, 2012

Getting the create Command to Work on PhoneGap 2.2.0 Android

Okay as of PhoneGap 2.2.0 the Getting Started guide has been "simplified" to make creating your first Phonegap Android project easier. Boy, that's not the  impression I get after helping people on the PhoneGap Google Group for the past week and a half.

From what I understand most of us Cordova core developers use a Mac as our main machine. Now I don't want to and won't start an OS holy war since there is a very good reason why we do that, you can't do iOS without a Mac and the tools for Android and BlackBerry development will run on a Mac so it makes sense for those of us who have to jump back and forth to be using a Mac. However, we've really dropped the ball in explaining what the proper setup needs to be when you are on a PC. So that's why I'm writing this blog post from my old Windows laptop that I dusted off so I can feel your pain.

Required Software
  1. Java Development Kit
  2. Apache Ant
  3. Android Development Kit
Go ahead and download and install those packages if you haven't already. I'll wait. I'm not up to anything else right now. But please pay attention to where you install these packages as you'll need those paths later.

Oh good, you're back.

First problem

Once you navigate to the phongeap\lib\android\bin directory the getting started guide tells you to run the following command:
./create <project_folder_path> <package_name> <project_name>
but that is not accurate on a Windows system as it won't be able to find the create command. The line should be:
create <project_folder_path> <package_name> <project_name>
Second problem

Once you do that you probably will see this error:


So basically and unhelpful error telling you that you are missing a package. We'll need to make sure the following commands can be run from the command prompt. Let's start by testing if the command line can find java.exe first. So type:
java
into the command line. If you see something like this:


then you will need to modify your PATH environment variable so that the script can find Java. First you'll need to discover where your JDK is installed. I can't tell you where that is so I hope you took my advice and noted where the installer placed those files. Armed with the path to your Java Development Kit do the following:

  • From the Desktop, right-click My Computer and click Properties. 
  • Click Advanced System Settings link in the left column. 
  • In the System Properties window click the Environment Variables button. 
  • Select the PATH variable from the System variables section. 
  • Select the Edit button.
which means you should have a dialog something like this:


This is where you will add the JDK\bin directory to your PATH. Notice you need to point to the bin directory where commands line java and javac are kept. For example "c:\jdk\bin". Also make sure to separate different path entries using a ";". Now you need to click OK then OK once more. After that close your command prompt and start a new one so the updated PATH variable will be used. In the new command prompt run:
java
once again and you should see something like this:


Now the create script will be able to find java. Unfortunately you'll need to repeat the process for the following commands:
  • javac
  • ant
  • adb
  • android
If you get the "blah" is not a recognized internal or external command error message for any of the above you'll need to add it to the PATH.

Third problem

The environment variable JAVA_HOME may be set incorrectly. Make sure that this variable is set and it refers to the root directory of your JDK and not JRE. The create.js script looks for %JAVA_HOME%\bin\javac.exe and if it can't find it there will be an error.

Fourth problem

Don't use '-' in your package name. The '-' is not a valid character in a Java package name.

Bad:    ./create ./name com.my-company.name name
Good:  ./create ./name com.mycompany.name name

Okay, I hope that helps everyone. I'm going to make the error messages more descriptive while I'm on my PC for the upcoming 2.3.0 release.


83 comments:

Chris Riesbeck said...

You shouldn't need to repeat the process for javac. It's in the same place as java.

Simon MacDonald said...

@Chris Riesbeck

Yes, I debated that but then I thought what if someone has the JRE in their path and can find "java' but not "javac" so I decided to leave it in.

Unknown said...

This worked great thank you.
Unfortunately, the create command does not seem to obey the project folder path in windows and will just place the project in the android bin folder where the "create" file is.

Simon MacDonald said...

@Unknown really? What kind of path are you using?

Kasper said...

In a command prompt these commands work:
ant
adb
java

and these are not working:
javac - though i've added its path to PATH C:\Program Files\Java\jdk1.7.0_09\bin

android - strange since adb is working and they are in the same folder

I have added both tools and platform-tools
C:\adt-bundle-windows-x86_64\adt-bundle-windows\sdk\tools
C:\adt-bundle-windows-x86_64\adt-bundle-windows\sdk\platform-tools

and as suggested, I also added:
C:\Program Files\Java\jdk1.7.0_09\bin

Any suggestions, as to how I solve this will be highly appreciated

Simon MacDonald said...

@Kasper

Double check that javac.exe is in the jdk folder and you will probably need to restart your command prompt if you made changes to the PATH.

Gerry Hernandez said...

All,

I have set the necessary path string to access java, javac, adb, android, and ant. When using a command prompt, I can use these tools individually without any problems. When using Cordova's "create" tool, I get the dreaded "missing one of the following" non-descript error.

None of them seem to be missing; I don't understand what's wrong.

Thanks in advance for your help :)

Simon MacDonald said...

@Gerry Hernandez

It looks like it is checking for JAVA_HOME as well. Is that environment variable set?

Amos Hayes said...

I'm past this install process, and can run the create command. But after following the instructions to add the project to eclipse (and trying to resolve the red X) I'm still left with a red X. The first in a series of errors is "The import org.apache.cordova cannot be resolved"

Any suggestions?

P.S. Also of note, if you "create" your project inside your workspace and then try to add it, it complains of an "Invalid project description." Crating it elsewhere and choosing to copy it into the workspace while adding it gets around that error.

Gerry Hernandez said...

Ah, that was it. Thanks!

It's funny how things just seem to work on Mac, but when I switch to Windows (which, for most enterprise development is *way* more developer friendly), I run into all these little problems. I guess I'm just spoiled by the Microsoft development stack; it's so easy, it's like comparing Mac and PC! Haha ;)

Simon MacDonald said...

@Amos Hayes

It sounds like you are missing the cordova-2.2.0.jar file in your project.

Amos Hayes said...

I was missing the jar. It wasn't getting built using the Cordova 2.2.0 download on ADT 4.2 bundle (on mac or PC).

I have since been able to build from the github master src on OSX (my work laptop) against the ADT 4.2.

I have given up on trying to get this running on Windows and am now about to install Ubuntu 12.10 on a VM so I can have a proper command-line-friendly OS while still using my nice gaming rig at home for development. The pain involved in getting anything like a pleasant shell experience on windows is too much! :)

Simon MacDonald said...

@Amos Hayes

Yeah, sorry. I plan on making create more verbose on windows for the 2.3.0 release to make life easier on our windows devs.

Amos Hayes said...

No worries! I thought I would give windows a try for development because my PC has a bit more umph and a lot more screen than my mac laptop. But in the end, I've come to the same sort of conclusion I had about running server apps on OSX. Not the right tool for the job. We'll see how I get on with an Ubuntu dev environment.

P.S. I noticed you were on holiday in Cape Breton! I love Cape Breton and have been there many times. Hope to get down again next summer.

Joshua Richards said...

when i run ant, i get Unable to locate tools.jar. Expected to find it in C:\Program Files\jre7\lib\tools.jar
Buildfile: build.xml does not exist!
Build failed

Joshua Richards said...

set java / ant and when i run ant i get the following:

Unable to locate tools.jar. Expected to find it in c:\program files\java\jre7\lib\tools.jar
Buildfile: build.xml does not exist!
Build failed

Any thoughts?

Joshua Richards said...

Got it sorted. Had to use set JAVA_HOME

Now I get "Invalid project description" Reason: "Invalid project description."

Joshua Richards said...

Just keeping everyone up to date:

1. Moved the files created from the "create" function to a different directory

2. Copied cordova-2.2.0.jar into the libs folder (from phonegap download, wasn't in the cordova download that is listed in the getting started doc)

Joshua Richards said...

After all of this. The app builds and it when I run on my device...

it hangs at

"Connecting to Device"

Joshua Richards said...

After some googling, found this, might be a good place to start for the docs.

Very straightfwd.

http://cleancodedevelopment-qualityseal.blogspot.ca/2012/11/how-to-install-phonegap-for-android.html

Philipp Schreiber said...

Though I was able to figure it out myself after some time, I appreciate that somebody wrote this up. Thanks Simon.

I also ran into the same problem as Amos, no cordova-2.2.0.jar in my project. It seems the jar is being removed every time the create process is run. (http://stackoverflow.com/questions/13399550/copying-cordova-2-2-0-jar)

After downloading it from the git repo and adding it to my project everything works fine. But the whole process leaves a very messy impression.

iknewthis said...

Thank you!!!

bin guo said...

Hi Simon,
Which Android SDK version should I use? I used Android 4.2 SDK, but the built apk can not work correctly on the emulator. I built on Windows 7.

thanks.
BR,
Rubin

Simon MacDonald said...

@bin guo

Usually I tell everyone to build with the latest SDK but right now I'd say hold off and stick with SDK 16, Android 4.1. There are bugs in SDK 17 that I expect should get fixed soon. The new @JavascriptInterface annotation just doesn't work and the bug where December is missing from the calendar make me think an SDK update will be released soon.

bin guo said...

Got it. Thank you!

BR,
Rubin

Grosche said...

I got one more issue: The create command worked fine when using C:\project-name or C:\Users\username\Documents\project-name but it didn't work if my workspace was located in Program files. I don't know if the problem was that "Program Files" contain a blank or if the UAC doesn't let you create paths in Program files. I got it working by changing create.bat and create.js a little:

1) In create.bat I deleted the \ in line 24, because %~dp0 contains the ending \ already.

2) In create.js I added a function GeneratePath(pFolderPath) which makes sure that all folders in the path that don't exist, will be created and replaced each call of fso.CreateFolder with the GeneratePath function

function GeneratePath(pFolderPath) {
var retval = false;
if(!fso.FolderExists(pFolderPath)) {
var path = pFolderPath.split("\\");
if(path.length>1) {
var joinedPath = path[0];
for(var i=1; i<path.length-1; i++)
joinedPath = joinedPath.concat("\\",path[i]);
if(GeneratePath(joinedPath)) {
retval = true;
fso.CreateFolder(pFolderPath);
}
} else if(path.length==1 && path[0].charAt(path[0].length-1)==":") {
retval = true;
fso.CreateFolder(pFolderPath);
}
} else {
retval = true;
}
return retval;
}

3) I changed the exec calls for the copying in lines 170-205 and made sure that the two arguments of the xcopy or copy command are wrapped with double quotes.

From:

exec('%comspec% /c xcopy "'+ ROOT + '"\\bin\\templates\\project\\res '+PROJECT_PATH+'\\res\\ /E /Y');

To:

exec('%comspec% /c xcopy "'+ ROOT + '\\bin\\templates\\project\\res" "'+PROJECT_PATH+'\\res\\" /E /Y');

4) I added an additional line to generate the src-path in line 174:

GeneratePath(ACTIVITY_PATH.split("\\"+ACTIVITY+".java")[0]);

Simon MacDonald said...

@Grosche

You should make a pull request against:

https://github.com/apache/incubator-cordova-android

So we can get your fixed into 2.3.0.

StevenInc said...

Simon, Thanks for taking the time to clear this up. Your post was extremely helpful.

StevenInc said...

Simon, After I worked out all the references in the path and each of the individual calls works, I ran:
C:\AndroidDev\Projects\MMS_v3

And Project already exists!
was returned, but the folder was empty.

Please advise.

StevenInc said...

Followup: I did have a project by the same name in my workspace.
Thanks ;)

Cabon Aurélie said...

In a command prompt these commands work:

adb
javat
javac

But no ant; I have this error
"blah" is not a recognized internal or external command error

I don't know why:
This is my path

C:\Program Files\Java\jdk1.7.0_09\bin;C:\Program Files\apache-ant-1.8.4\ANT\bin;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools;

I tried everything with and without environnement variable.

Cabon Aurélie said...

Ok now everything works individually :
java
javac
adb
android

But when I want to create a new project there is an error:

"Missing of the following..."
I really don't understand

Jordan Haddow said...

Thank you so much. I wasted so much time trying to figure out that stupid error message. I had JRE installed and so java worked fine. I didn't realise until I read your article that it was because javac wasn't being found.

Mahesh said...

I got all the other commands to work, but now when I try to run the create command, I get this error:

'cscript' is not recognized as an internal or external command, operable program or batch file.

Simon MacDonald said...

@Mahesh

cscript.exe is a key windows component. It should be installed on your machine. If not you'll have to track down the installer for your version of windows.

Simon MacDonald said...

@Cabon Aurélie

Weird, make sure your JAVA_HOME environment variable is not pointing to a JRE installation. It should point to C:\Program Files\Java\jdk1.7.0_09 in your case.

Kautsky Lozano V. said...

Hey Simon, I want to say: Thank You! :)

Tina Behrens said...

Hi, I'm having the same problem as @Cabon Aurélie... I can run all the commands (java, javac, ant, adb and android) but when I try to create I get the "The term 'create' is not recognized blabla" error. I have set JAVA_HOME to C:\Program Files\Java\jdk1.7.0_09 and my PATH looks like this ;C:\Program Files\adt-bundle-windows\sdk\platform-tools;C:\Program Files\adt-bundle-windows\sdk\tools;%JAVA_HOME%\bin;%ANT_HOME%\bin;%ANDROID_HOME%;%ANDROID_HOME/tools%;%ANDROID_HOME%/platform-tools;

I have checked that the create.bat is in the location I access:
cd 'C:\Program Files\cordova\android\bin'
create Z:\workspace\app com.mycomany.App App

Help!

Rhod said...

I have gotten pass the various errors and thought I was home free. I am, however, getting a "Project already exists!" error no matter what project name I use

Unknown said...

Worked fine.

Thank you.

dave said...

Thank you!

J Billa said...

This isn't related to the Windows nature of this post, but I haven't been able to find any posts that were as well written as this, so I thought I'd throw the question out there and see if maybe anyone has had a similar issue.

I'm having a problem on OSX 10.8.2. I use the oh-my-zsh BASH in iTerm. I've updated 'export PATH' in my .zhsrc (the .bash_profile equivalent for oh-my-zsh) with both my 'platform-tools' and 'tools' folders. I've followed the instructions as it explains in the Cordova 2.2.0 documentation. Yet I keep getting the false error 'Project already exists! Delete and recreate'. This isn't the case because I could put anything in and it keeps saying the same thing. I've been knocking my head against the table trying to figure this out with no luck. Any help would be appreciated. Thanks!

J Billa said...

Also, I forgot to mention, I am cd'ing into the bin directory and using the create command.

Simon MacDonald said...

@J Billia

Well I don't do iOS that often but you should check out Becky's updated instructions from this thread. She attached a PDF of them.

https://groups.google.com/d/topic/phonegap/36eXgx2LrNI/discussion

DW90 said...

'cscript' is not recognized as an internal or external command, operable program or batch file.
I know that this has been posted before, however a solution was not presented. I was wondering if you have a solution for this?
Thanks

Simon MacDonald said...

@DW90

Make sure you have Windows Scripting Host installed. If you do then you need to make sure that the path to cscript.exe is in your PATH environment variable.

Summved Jain said...

Hello Simon,

I am sorry that I am asking the below question on this post. But I am stuck in between, I have upgraded to Cordova 2.2 and now Map in my app is not working. I am getting below error.

01-12 17:43:43.765: E/SQLiteLog(4899): (14) cannot open file at line 30174 of [00bb9c9ce4]
01-12 17:43:43.773: E/SQLiteLog(4899): (14) os_unix.c:30174: (2) open(/GeolocationPermissions.db) -
01-12 17:43:43.773: D/WebKit(4899): ERROR:
01-12 17:43:43.773: D/WebKit(4899): SQLite database failed to load from /GeolocationPermissions.db
01-12 17:43:43.773: D/WebKit(4899): Cause - unable to open database file
01-12 17:43:43.773: D/WebKit(4899): external/webkit/Source/WebCore/platform/sql/SQLiteDatabase.cpp(71) : bool WebCore::SQLiteDatabase::open(const WTF::String&, bool)


Kind Regards,
Summved

pikaaa said...

The way to set JAVA_HOME was pretty hard for me to understand at first. I read a post on stackoverflow and solved my problem.

In the environment,

1)Create a new system variable JAVA_HOME

2)Set the value to C:\Program Files\Java\jdk1.7.0\

Simon MacDonald said...

@Summved Jain

Sorry, I'm not as well versed with iOS as I am with Android. You'd be better off asking your question on the PhoneGap google group.

Summved Jain said...

Hello Simon,

This is coming on the Android not on the IOS.

Kind Regards,
Summved

Darnell Shumpert said...

Hey Simon,

When I type javac, java, adb, android, or ant, I have no errors and it displays what it is suppose to in the command prompt. When I try to create a project though, it still says that its missing on of the following.

Any advice?

Thanks in advance

Simon MacDonald said...

@Darnell Shumpert

Make sure JAVA_HOME and ANT_HOME are set properly.

saadu said...

Thanks for javac check, till i read your post i have not noticed my java_home is pointing to jre :(

Todilo said...

I did also have a problem with javac, fixed by setting the PATH to the jdk folder\bin . Java command works with JAVA_HOME but javac doesn't. I must say though that the documentation is still terrible. I have followed the steps letter by letter and now used the create-script and well it is missing the cordova.jar file as the phonegap cant be imported!!! What the!!

Michael Schmidt said...

Thanks for this description. I have done all the steps and came a lot further. But the create-command now results in a

CScript error: script module "JScript" for script "C:\phonegap-2.3.0\lib\android\bin" was not found.

(I translated this message from German, so the english version may vary)

As I understood so far my cscript can't interpret *.js files. Do you know how I can change this. I reset Windows 7 (64Bit) to factory status some days ago.

Michael Schmidt said...

I found the solution ...

Open command line, go to C:\windows\system32 and type

regsvr32 jscript.dll

to reregister the dll.

mooreds.com said...

Hi folks,

I ran into real issues if I used this:
create.bat ./projnamedir package projname
vs
create.bat projnamedir package projname

The projnamedir would be created, but none of the template files were correctly copied.

I think that you can't use forward slashes as part of the path.

Simon MacDonald said...

@mooreds.com

Such are the differences between PC's and Mac OS/UNIX. Most of us are on Mac's but I tried to update the docs site with instructions for PC users.

Brett Upton said...

Using 2.4.0 and running the create command in Windows has no errors and shows me that all the js and jar files are created/built successfully but when I go to the created folder there is no jar there.

Simon MacDonald said...

@Brett Upton

Sorry, I don't even have a PC to test this anymore. Best to post a question on the Google Group.

Enrico Masin said...

Simon, I have a big problem!!
I suggest that i've correctly defined the paths in the "System Variables" section, but again the message "Missing one of the following..." is appearing!!
If i type java, a long list of command appears (link it should); if i type javac, the same; if i type adb, the Android debug starts.
The problem seems to be the ant: if i type it, the prompt says: "Unable to locate tools.jar. Expected to find it C:\Program Files\Java\Jre7\lib\tools.jar build.xml does not exists!"

What's the problem??

Thanx for your answer.

Simon MacDonald said...

@Enrico Masin

Your JAVA_HOME variable is set incorrectly. It is pointing to a JRE when it should point to a JDK. Wherever you javac binary is that is where your JAVA_HOME should point to minus the bin folder.

Chris Alfred said...

The path to you project can not have a space in the name e.g. Eclipse may have your workspace in C:\Users\User Name\workspace and your User Name may have a space in it.

You can use a symbolic link to your work space directory by using MKLINK /D LINK_NAME PATH.

Thorsten Harbig said...

be sure to start the command line as administrator when you register jscript.dll

Svetoslav Marinov said...

Thanks Simon for the cool article.
I thought I had the android SDK already installed...

Adam said...

Just what I needed. THANKS!

ShaunnyBwoy said...

Hi Simon,

I've read through your well written article, and all of the posts. You have to be the most patient developer in the world. I can imagine you going on a rampage in down town NY if you should ever repeat this phrase once more!

"Check that you have set the environmental variable JAVA_HOME"

I do have one problem that frustrates me, if you have the will to help any longer :)

I'm running Windows 7 64 Bit, and though I have successfully installed:

Android Development Toolkit
Java Development Environment
Apache ANT (Another Neat Tool - Looked that one up!)

as well as set all the variables for:
%ANT_HOME% which points to C:\eclipse folder\eclipse\plugins\org.apache.ant_1.8.3.v20120321
%JAVA_HOME% which points to C:\Program Files\Java\jdk1.7.0

and I can run:
java
javac
ant
adb
android


as well as restarted to prompt, but I'm still apparently missing something when running the phonegap "create" command.

Any help you can provide would be most appreciated!

Simon MacDonald said...

@ShaunnyBwoy

You've explained what you've done right but you haven't said what error you are getting.

ShaunnyBwoy said...

Hi Simon,

Sorry I wasn't clear. What I meant was that I get the "missing one of the following:" messages when running the create command. I can't see what I could be missing, or what I'm doing wrong. I've downloaded version 2.5 of PhoneGap if that helps anything.

Simon MacDonald said...

@ShaunnyBwoy

Post up exactly the create command you are using and I'll see if I can spot anything.

Admin said...

Hello Simon,

Thank you for nice article. I was getting same error as you have explained here and finally I am able to run first apps using PhoneGap.

Thanks Alot..

safvan7 said...

C:\phonegap\lib\android\bin>./create D:\android\new com.example.ball ball
when i hit enter after this, it shows "project already exist !" please help me to solve this problem

Justin Foley said...

Thank you so much for this post! Huge help.

Simon MacDonald said...

@safvan7

If the directory D:\android\new already exists then that is probably why you already have that error.

antares said...

Thank you for this tutorial. This should be the official getting started.

Hélio said...

After too many hours crying, I solved my problem.

In my Windows 8, cscript say that .js files are unknown.

Open command line as admin, go to C:\windows\system32 and type

regsvr32 jscript.dll

In create.bat, replace something like:

cscript "%~dp0\create.js" %*

to

cscript //E:jscript "%~dp0\create.js" %*

Jesus I lose too many time to discover this!!!! arrrrrrrrrrrrrg

"create" command will works now...

Leoš Literák said...

I had two issues:

create did not found ant, because I had a " character in PATH variable

create failed copying files because of hyphen (-) in project path (thanks Greg)

Adam Lofting said...

Thanks! This plugs some gaping wholes in the getting started guide.

Anurag Acharya said...

Thanks so much. Your article really helped to run this on windows!!

- Anurag

Todd V. said...

'Project Already Exists!' -- Solution -- for me it was because I was choosing a folder that was already create ... i.e. create/C:/MyFolder --- MYFOLDER cannot already exist! Put a path to a folder that DOES NOT EXIST

ie. create C:/MyFutureFolder ...

Puneet Gupta said...

@ Simon MacDonald
Thanks for your help.This doc help a lot..

garthwaite said...

Nice post Simon. It helped me set up my environment.

However, the "create" command returned the following error:

Input Error: There is no script engine for file extension ".js"

I googled around and someone suggested entering the following at the command prompt:

assoc .js=JSFILE

which I did and the create then worked.


nuttykhunshan said...

@Michael Schmidt

thanks a lot. I had jscript.dll banging m dead head