Export Cocos2d-js html 5 games

Hello everyone, I have started working with cocos2d-js for web html 5 only. My question is, after I finish the game, how do I export it and what should I send to the clients, because I would not like to sell my source code, so I would like not to have to send them the javascript files. There is no problem with the graphic files since they are rasterized and combined in spritesheets, but having to send the js files to my clients could be a deal breaker for using this engine.

I’m wondering how everyone else is distributing their html 5 games or if anyone has a good solution for my problem.

Thanks

Hi~
JS is a scripting language.
So there is no way to compile a language.
If you have the need to hide their own code, can try advanced compression mode.
It can hide part of the code, make the code more difficult to read.
http://www.html5china.com/thread-1231-1-1.html
Here is a Chinese document…

Hello,

I would like to add to this question.

Previously, there was a way to compress and hide a little bit code:
In html5 one can put build.xml into the project and compiler.jar used to generate compressed game in single file (together with cocos-xxxxxx-min.js).

Now I can find these files in frameworks\cocos2d-html5\tools, but readme for tools.txt points that there is wiki page on closure compiler from google at cocos2d, however this page does not exist. Not much information can be found in cocos2d site about it anymore.
Does it mean that cocos2d-js would not support closure? I can see in the directory few new scripts which I do not know about. (core4cc.js, genBuildXML.js publish.js).

Also there is option to compile web: cocos compile -p web -m release, but it always produce as output:

Runing command: compile
Building mode: release
Not valid jdk isntalled

I have tried to install jdk and update environment variables with 1.8, 1.7 (x86 and 64), 1.6 (x86 and 64). In all cases “Not valid jdk istalled”. My guess was that compile a web should produce the result which I expected, but unfortunately I do not know how to make it work.

Hi laser,

So a part of my question was answered here What to upload to web sever for deploying a cocos2d html5 project? . Pandamicro explained in his post how to use the cocos compile command and there is also the builder tool here http://cocos2d-x.org/filecenter/jsbuilder/ that lets’s you download the whole engine in just one .js file.

Now I haven’t tried yet the cocos compile command so I don’t know how it works and what it exports but assuming that the command does it’s job and outputs a minimum of js files to upload then we only need a solution to hide our code from the clients of the games or for whoever knows how to read a javascript file.
One solution to this problem is JScrambler, a tool that you can find here https://jscrambler.com that lets you protect and obfuscate your code.

For now, if I’ll manage to use the compile command to reduce the number of js files, that, combined with the JScrambler tool is good enough for me, hope it helps you too,

Which platform are you using ? normally it should work with jdk 1.6 and 1.7, and what you get from java -version ?

I get the same error:

Mac-Pro-di-Ale:MyJSGame ale$ cocos compile -p web -m release
Runing command: compile
Building mode: release
Not valid jdk isntalled

Both on mac (mavericks 10.9.4) and windows (8.1).
Also tried redownloading CocosJS V3.0RC1, and created a new base project, got the same error.

On mac:

Mac-Pro-di-Ale:MyJSGame ale$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

On win:

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

Even with java sdk 7, same error.

I haven’t used JavaScript for cocos, but since it’s just JavaScript, I’d recommend the super-popular UglifyJS.

In the web world, it’s normal to have a build system that consists of minifying, concatenating, etc. Plugins allow you to do this with file watching so it’s seamless and automatic.
My personal favorite build system is Gulp, although Grunt is another popular (and older) alternative.

Thank you Jgod.
I tryed UglifyJS in the past, but i prefer closure compiler in advanced mode, it seems to optimize a bit (sometimes a lot) better.

About the previous error, found a solution: it was my mistake on installing jdk 7 but not setting it correctly.
If anybody get the same error, just install jdk 7 and set it as default; for windows, on prompt:

set JAVA_HOME=C:\Programmi\Java\jdk1.7.0_65
set PATH=C:\Programmi\Java\jdk1.7.0_65\bin;%PATH%

obviously, use your own path instead of “C:\Programmi\Java\jdk1.7.0_65”

This is the first time I’m hearing about Closure, interesting :slight_smile:

Hello,

Sorry, I was quite busy with other staff. Now back to the project.

I do not know what it showed back then, but now with a new installed system I am receiving the same message.

java -version shows

java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)

I also tested with x64 version of 1.7.

java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

One note: I had to put link to JDK as a first line in PATH variable, as there is also system32 inside PATH and somehow my Win7 contains java.exe file in system32 (which shows 1.8).

PS: I do not have any need right now for web app, but out of curiosity want to resolve the issue.

It’s true that we only have Closure Compiler compiled using 1.6 and 1.7, it’s really painful that the jdk couldn’t be backward compatible on Closure Compiler. Anyway we will support 1.8 in the future version

So with jre 1.7, you still have the same error ?

If you mean jdk 1.7, then yes. The error remain the same.

C:\Users\laser\cocos\cocos2d-js-v3.1\test1>java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)

C:\Users\laser\cocos\cocos2d-js-v3.1\test1>cocos compile -p web -m release --advanced
Running command: compile
Building mode: release
Not valid jdk installed

It’s really odd that the sub version also affect the problem, because @alek76 have succeed with jdk1.7.0_65. I will check it with cocos console developer monday, but maybe you can try with jdk1.7.0_65. Sorry for the inconvenience

Did not help. However I have found the “build_web_init_.py” file, and run by hand:

C:\Users\laser>java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b19)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)

C:\Users\laser>python
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win    32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> child = subprocess.Popen(["java", "-version"], stderr=subprocess.PIPE)
>>> for line in child.stderr:
...     print line
...
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
>>>

Somehow, python sees another “java.exe” file which is inside “system32”. And quick search indicates that the file in system32 is dummy: http://stackoverflow.com/questions/11063831/what-is-the-difference-between-system32-java-exe-and-program-files-java-jdk1

I can completely remove “system32” from “Path” environment variable, but prefer to avoid that.

BR,
Andrey.

1 Like

Solved. I have made a modification to cocos-js, namely:

cocos2d-js-v3.1\tools\cocos2d-console\plugins\project_compile\build_web\__init__.py

The line

child = subprocess.Popen(commands, stderr=subprocess.PIPE)

changed to

child = subprocess.Popen(commands, stderr=subprocess.PIPE, shell=True)

As it was mentioned here: http://www.bogotobogo.com/python/python_subprocess_module.php

The reason is following: it seams that with shell command the process is run on top of intermediate shell which inherit all envrionment variables, while without it uses unknown own (or run without?). And without environment it checkes default paths (i.e., “system32”), where dummy “java.exe” exists.

Verified with “ant”, which is inside my OS “system variables”:

C:\Users\laser>python
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("ant")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Program Files\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Program Files\Python27\lib\subprocess.py", line 958, in _execute_chil
d
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> subprocess.call("ant", shell=True)
Buildfile: build.xml does not exist!
Build failed
1

PS: also it is recomended to run “call” instead of “Popen” on subprocess… however in sourcecode the “call” is “popen(…).wait()”

2 Likes

@Iaser You are great ! Thanks for digging so deep for this problem. It can really help us to improve the tool.
@zhangbin Please check the ressources that @laser have given in his last post, and make proper changes to cocos console. Thanks

@all, Here is a pull request of cocos2d-console which can solve the problem of compatibility of JDK 8.
I’ve tried it. It works well.

@laser, Thanks for your feedback! I will fix it later.

For me the issue was that I had installed java 1.7 and 1.8. I uninstalled all java 1.8 from windows and now the compile command works well.