Skip to content
Prev Previous commit
Next Next commit
2 of 2 net.sf.j2s.core_3.2.1.jar adds Java 8 functionality (new files):
 the "::" operator
 lambda expressions
 java.util.function.*
 java.util.stream.*
 default methods in interfaces
  • Loading branch information
hansonr authored and hansonr committed Jul 20, 2018
commit cea4ced495e617e808d158e16ca807fffa5153aa
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
1 change: 1 addition & 0 deletions sources/net.sf.j2s.core/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The seed Eclipse project site/ directory for all SwingJS projects can be found a

https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.java.core/SwingJS-site.zip?raw=true

this file is old. See dist/dropins/README.txt

Known issues: 2/2018

Expand Down
117 changes: 86 additions & 31 deletions sources/net.sf.j2s.core/dist/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,32 @@ SwingJS distribution

https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.core/dist/dropins/README.txt

7/15/2018 Bob Hanson hansonr@stolaf.edu
7/20/2018 Bob Hanson hansonr@stolaf.edu

The base Java version is Java 6. However, many of the functionalities of Java 7 and Java 8 are included.
This includes

the "::" operator
lambda expressions
java.util.function.*
java.util.stream.*
default methods in interfaces

All projects compiled under 3.1.1 need to be recompiled using the Java2Script 3.2.1 transpiler, as
described below. Note that if you use /** @j2sNative */ calls to interface methods that are singlets,
you no longer need to add qualifications (such as $O) to them. It should be no problem to leave them
qualified, though, as the transpiler creates qualified and unqualified aliases.

There are situations where this use of unqualified method names can run into problems. For example, in
java.util.stream.ReferencePipeline, there are three different declarations of the functional interface
method "accept". They were just for erro reporting and have been removed.
My guess that this is a rarity, but I am not sure.


SwingJS has been successfully tested in Eclipse version Neon-Photon on Mac and Windows platforms.
(No reason to believe it would not also work for Linux; just haven't tried that recently.)
Java 8 is the target Java version for transpilation. Please report any missing classes or strange
errors.

net.sf.j2s.core_3.2.1.jar replaces net.sf.j2s.core_3.1.1.jar

Expand All @@ -11,7 +36,7 @@ Most importantly, the requirement that the .project file be changed to
indicate a customized Java builder is dropped in version 3.2.1, which just uses
the standard Eclipse java builder, org.eclipse.jdt.core.javabuilder.

Thus, to get started, all you need are:
For v. 3.2.1, to get started with SwingJS, all you need are:

1) the latest transpiler from

Expand All @@ -31,8 +56,6 @@ or your project will no longer compile.

Comments below have been adjusted for these differences.

SwingJS has been successfully tested in Eclipse version Neon-Photon on Mac and Windows platforms.
(No reason to believe it would not also work for Linux; just haven't tried that recently.)

INSTALLATION INSTRUCTIONS

Expand Down Expand Up @@ -77,6 +100,10 @@ On Mac systems, the Eclipse directory is generally
2. Restart Eclipse and check for the presence of the plug-in at
help...about Eclipse...installation details...Plug-ins...(wait several seconds for tabulation)

Search for "j2s" to find j2s.sourceforge.net Java2Script Core

If that is not there, you don't have net.sf.j2s.core.jar in the proper directory.

Note relating to updating to 3.2.1 from 3.1.1 version of Java2Script:

If the version on this readout does not match the version that is
Expand All @@ -88,10 +115,6 @@ On Mac systems, the Eclipse directory is generally
Eclipse once with the -clean flag.


search for "j2s" to find j2s.sourceforge.net Java2Script Core

If that is not there, you don't have net.sf.j2s.core.jar in the proper directory.


----------------------------------
Creating a new J2S/SwingJS project
Expand All @@ -101,7 +124,7 @@ Create an Eclipse Java project for your work, if you have not done so already.
If your source code is not all already in src/, navigate to the project...properties...
Java Build Path...source and add all the source directories you need.

Note that your project must not include any Jar file based dependencies.
Note that your project must not include any Jar file-based dependencies.
All source code must be available. (Source code from decompiling .class files will work.)


Expand All @@ -124,36 +147,32 @@ Simply download and unzip that file into your project, creating a top-level site
Enabling the Java2Script/SwingJS transpiler
-------------------------------------------

1. Create in your Eclipse project the file:
1. Create in your Eclipse project the empty file:

.j2s

containing simply:
The next time you build the project, the transpiler will see that and
add to it:

j2s.compiler.status=enable
j2s.site.directory=site

2. Edit the .project file to indicate that the j2s transpiler is to be used
rather than the standard Java compiler by changing the buildSpec buildCommand from

org.eclipse.jdt.core.javabuilder

to

net.sf.j2s.core.java2scriptbuilder

along with several other commented-out options. If you want, you can
add these two yourself before you run the transpiler.

--------------------
Building the project
--------------------

Build your project as you normally would. Java class files will be created as usual in the bin/ directory.
JavaScript equivalents of these files will be created in the site/swingjs/j2s directory. You might have to
do a project refresh to see these site files.
do a project refresh to see these site files. If you open one in Eclipse, notice that each time you build
the project (for example by saving a changed Java file with Project...Save Automatically set), Eclipse
will prompt you to reload this file with changes.

Do take a look at the .js files created. You will notice that they are all the methods and fields of your
Java project *except* final static constants. SwingJS does not recreate those by name; it just uses them.
(The only thing this should affect is that java.lang.reflect.Field does not indicate these names.)

Java project *except* final static constants. SwingJS does not recreate final static constants by name;
It just uses them. This means that the java.lang.reflect.Field methods will not indicate these names.

----------------------------------------------
Testing the JavaScript version of your project
Expand All @@ -163,7 +182,34 @@ The J2S transpiler will automatically set up for you in site/ a sample HTML page
that subclasses JApplet or contains a public void main(String[] args) method. You will want to
associate those files with an external HTML browser. We recommend Firefox.

Since you will be running AJAX locally within these browsers, you may need to enable local
Do not change these files, as they will be recreated each time the transpiler runs. If you want
a different configuration -- a different width or height, or some additional "applet" parameters,
make a copy of this file and change within it the Info block:

Info = {
code: _CODE_,
main: _MAIN_,
width: 850,
height: 550,
readyFunction: null,
serverURL: 'https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php',
j2sPath: 'swingjs/j2s',
console:'sysoutdiv',
allowjavascript: true
}

These Info key/value pairs are equivalent to Java applet parameters. Use Info.args for the main(args[])
parameters. For instance:

args:["test","true"],

The serverURL reference allows for reading files in AJAX from servers that are not configured with

access-control-origin: *

You can try removing that if you want to.

Since you will be running AJAX locally within your browser, you may need to enable local
file reading in your browser. Instructions for doing that can be found at
http://wiki.jmol.org/index.php/Troubleshooting/Local_Files

Expand All @@ -176,9 +222,13 @@ If you find you are missing a Java class, please contact me (Bob Hanson) at hans
You can try adding these yourself by **temporarily** adding one or more of the Java classes found
at http://grepcode.com to the proper package in your project. For example, java/awt.

If you do that, be sure to use the OpenJDK version. Most of the code in the SwingJS project started with
Java 6-b14 or 6-b27. Build your project, then delete these Java files, because you do not necessarily
want your Java code using that version, just JavaScript.
If you do that, be sure to use the OpenJDK version. For example:

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes

Most of the code in the SwingJS project started with
Java 6-b14 or 6-b27. Build your project, then delete these Java files, because, should this file be added
to an updated verison of SwingJS, you should probably use the one provided, not your own. Your choice.


----------------
Expand All @@ -198,15 +248,20 @@ working site is https://chemapps.stolaf.edu/swingjs/physlets

Phet applets have not been put on GitHub yet.

Note that these sites use an older v. 3.1.1 transpiler and runtime.

Feb. 17, 2018

As of Feb. 17, 2018, we are actively converting a variety of functioning Java applets.
We are actively converting a variety of functioning Java applets.

The physlets Animator, Doppler, and Optics are working.

As of Jan. 2, 2018, all known Java-to-JavaScript issues have been dealt with.
Jan. 2, 2018

Efficient Google Closure Compiler compression is in place.

As of Dec. 17, 2017, SwingJS is fully operational in its "version 2" format,
Dec. 17, 2017

SwingJS is fully operational in its "version 2" format (still v. 3.1.1),
which includes fully qualified method, array, and number typing.

1 change: 1 addition & 0 deletions sources/net.sf.j2s.core/dist/dropins/timestamp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2018-07-19_0557 AM
98 changes: 98 additions & 0 deletions sources/net.sf.j2s.java.core/src/java/lang/AutoCloseable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

package java.lang;

/**
* An object that may hold resources (such as file or socket handles)
* until it is closed. The {@link #close()} method of an {@code AutoCloseable}
* object is called automatically when exiting a {@code
* try}-with-resources block for which the object has been declared in
* the resource specification header. This construction ensures prompt
* release, avoiding resource exhaustion exceptions and errors that
* may otherwise occur.
*
* @apiNote
* <p>It is possible, and in fact common, for a base class to
* implement AutoCloseable even though not all of its subclasses or
* instances will hold releasable resources. For code that must operate
* in complete generality, or when it is known that the {@code AutoCloseable}
* instance requires resource release, it is recommended to use {@code
* try}-with-resources constructions. However, when using facilities such as
* {@link java.util.stream.Stream} that support both I/O-based and
* non-I/O-based forms, {@code try}-with-resources blocks are in
* general unnecessary when using non-I/O-based forms.
*
* @author Josh Bloch
* @since 1.7
*/
public interface AutoCloseable {
/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
*
* <p>While this interface method is declared to throw {@code
* Exception}, implementers are <em>strongly</em> encouraged to
* declare concrete implementations of the {@code close} method to
* throw more specific exceptions, or to throw no exception at all
* if the close operation cannot fail.
*
* <p> Cases where the close operation may fail require careful
* attention by implementers. It is strongly advised to relinquish
* the underlying resources and to internally <em>mark</em> the
* resource as closed, prior to throwing the exception. The {@code
* close} method is unlikely to be invoked more than once and so
* this ensures that the resources are released in a timely manner.
* Furthermore it reduces problems that could arise when the resource
* wraps, or is wrapped, by another resource.
*
* <p><em>Implementers of this interface are also strongly advised
* to not have the {@code close} method throw {@link
* InterruptedException}.</em>
*
* This exception interacts with a thread's interrupted status,
* and runtime misbehavior is likely to occur if an {@code
* InterruptedException} is {@linkplain Throwable#addSuppressed
* suppressed}.
*
* More generally, if it would cause problems for an
* exception to be suppressed, the {@code AutoCloseable.close}
* method should not throw it.
*
* <p>Note that unlike the {@link java.io.Closeable#close close}
* method of {@link java.io.Closeable}, this {@code close} method
* is <em>not</em> required to be idempotent. In other words,
* calling this {@code close} method more than once may have some
* visible side effect, unlike {@code Closeable.close} which is
* required to have no effect if called more than once.
*
* However, implementers of this interface are strongly encouraged
* to make their {@code close} methods idempotent.
*
* @throws Exception if this resource cannot be closed
*/
void close() throws Exception;
}
Loading