Pass argument via JNLP file into java web start application - java

I'm beginner in Java programming, and I'm trying to make Java Web Start app. I need to pass some args to application, trough JNLP. When I hardcode values in web app, JNLP without args works fine, and start application. Also, when launch application from cmd with arguments, it's working. So I think that problem is in the JNLP file. When I try to launch app with JNLP that contains arguments, start failed with error:
java.lang.ArrayIndexOutOfBoundsException: 0
JNLP file looks like:
<application-desc main-class="SpeedScanLaunch" >
<argument>http://localhost/TestApp/Dam/</argument >
<argument>x</argument >
<argument>50004</argument >
</application-desc >
and Java class looks like:
import java.io.*;
import javax.swing.JOptionPane;
public class SpeedScanLaunch
{
public static void main(String args[])throws IOException
{ String p = args[0];
String t = "/type:" + args[1];
String id = "/id:" + args[2];
JOptionPane.showMessageDialog(null, p);
JOptionPane.showMessageDialog(null, t);
JOptionPane.showMessageDialog(null, id);
...
Does anyone know what the problem with JNLP is?
In addition, screnn freom JaNeLa:
Screenshot from janela
And text report from JaNeLa:
JaNeLA Report - version 11.05.17
Report for file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp
Content type application/xml does not equal expected type of application/x-java-jnlp-file
cvc-complex-type.2.4.d: Invalid content was found starting with element 'security'. No child element is expected at this point.
cvc-complex-type.2.4.d: Invalid content was found starting with element 'security'. No child element is expected at this point.
XML encoding not known, but declared as utf-8
Codebase '.' is a malformed URL! Defaulting to file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp
Codebase + href 'file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch.jnlp' is not equal to actual location of 'file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp'.
Optimize this application for off-line use by adding the <offline-allowed /> flag.
Codebase '.' is a malformed URL! Defaulting to file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp
Codebase '.' is a malformed URL! Defaulting to file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp
Codebase '.' is a malformed URL! Defaulting to file:/E:/eclipse_workspace/TestProjekt/bin/SpeedScanLaunch1.jnlp
Downloads can be optimized by specifying a resource size for 'SpeedScanLaunch.jar'.
The resource download at SpeedScanLaunch.jar can be optimized by removing the (default) value of download='eager'.
The resource download at SpeedScanLaunch.jar can be optimized by removing the (default) value of main='false'.
It might be possible to optimize the start-up of the app. by specifying download='lazy' for the SpeedScanLaunch.jar resource.
Lazy downloads might not work as expected for SpeedScanLaunch.jar unless the download 'part' is specified.

Related

Why Scala on Mac fails when run in unicode directory

I am attempting to simply compile a helloworld scala file from within a directory named using unicode characters but the scala compiler fails to run. It succeeds when I change directory's out the unicode named folder.
Detail
Given a classic Helloworld.scala class such as the one below
that is located in the directory:
/Users/me/Dev/Company/𝔘𝔫𝔦𝔠𝔬𝔡𝔢/code_folder
I am unable to run scalac on the code if my current directory is in code_folder which is the root directory for the Helloworld.scala. When I change directories out of the folder with the unicode name /𝔘𝔫𝔦𝔠𝔬𝔡𝔢, the compiler works just fine on the same code. So it would seem that scalac doesnt work when attempting to compile code while in a folder with unicode characters.
Why is that? Is this a system error? An unparsed unicode error between the OS starting the scalac process?
object Main{
def main(args: Array[String]): Unit = {
println("Hello world")
}
}
I expected a compiled file. The error I get is this
Exception in thread "main" java.lang.ExceptionInInitializerError
at scala.tools.util.PathResolver$Environment$.scalaExtDirs(PathResolver.scala:77)
at scala.tools.util.PathResolver$Defaults$.scalaExtDirs(PathResolver.scala:127)
at scala.tools.nsc.settings.StandardScalaSettings.$init$(StandardScalaSettings.scala:31)
at scala.tools.nsc.settings.MutableSettings.<init>(MutableSettings.scala:28)
at scala.tools.nsc.Settings.<init>(Settings.scala:19)
at scala.tools.nsc.Driver.process(Driver.scala:53)
at scala.tools.nsc.Driver.main(Driver.scala:80)
at scala.tools.nsc.Main.main(Main.scala)
Caused by: java.lang.IllegalArgumentException: Error decoding percent encoded characters
at java.base/sun.net.www.ParseUtil.decode(ParseUtil.java:209)
at java.base/jdk.internal.loader.FileURLMapper.getPath(FileURLMapper.java:64)
at java.base/jdk.internal.loader.FileURLMapper.exists(FileURLMapper.java:73)
at java.base/jdk.internal.loader.URLClassPath$JarLoader.getJarFile(URLClassPath.java:802)
at java.base/jdk.internal.loader.URLClassPath$JarLoader.access$900(URLClassPath.java:692)
at java.base/jdk.internal.loader.URLClassPath$JarLoader$1.run(URLClassPath.java:751)
at java.base/jdk.internal.loader.URLClassPath$JarLoader$1.run(URLClassPath.java:744)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/jdk.internal.loader.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:743)
at java.base/jdk.internal.loader.URLClassPath$JarLoader.<init>(URLClassPath.java:718)
at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:486)
at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:469)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:468)
at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:437)
at java.base/jdk.internal.loader.URLClassPath.findResource(URLClassPath.java:280)
at java.base/jdk.internal.loader.BuiltinClassLoader.findResourceOnClassPath(BuiltinClassLoader.java:479)
at java.base/jdk.internal.loader.BuiltinClassLoader.findResource(BuiltinClassLoader.java:303)
at java.base/java.lang.ClassLoader.getResource(ClassLoader.java:1393)
at java.base/java.lang.ClassLoader.getSystemResource(ClassLoader.java:1658)
at java.base/java.lang.ClassLoader.getSystemResourceAsStream(ClassLoader.java:1762)
at java.base/java.lang.Class.getResourceAsStream(Class.java:2607)
at scala.util.PropertiesTrait.scalaProps(Properties.scala:39)
at scala.util.PropertiesTrait.scalaProps$(Properties.scala:37)
at scala.tools.reflect.WrappedProperties$AccessControl$.scalaProps$lzycompute(WrappedProperties.scala:49)
at scala.tools.reflect.WrappedProperties$AccessControl$.scalaProps(WrappedProperties.scala:49)
at scala.util.PropertiesTrait.scalaPropOrNone(Properties.scala:71)
at scala.util.PropertiesTrait.scalaPropOrNone$(Properties.scala:71)
at scala.tools.reflect.WrappedProperties$AccessControl$.scalaPropOrNone(WrappedProperties.scala:49)
at scala.util.PropertiesTrait.$init$(Properties.scala:83)
at scala.tools.reflect.WrappedProperties$AccessControl$.<init>(WrappedProperties.scala:49)
at scala.tools.reflect.WrappedProperties$AccessControl$.<clinit>(WrappedProperties.scala)
... 8 more
Version:
scalac -version
Scala compiler version 2.12.8 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.
MacOS version 10.13.6
Not really a solution, but some piece of information:
I set up my folder this way:
/home/nicolas/Private/𝔘𝔫𝔦𝔠𝔬𝔡𝔢/
When the ClassLoader tries to load the classes of your project it makes a list of paths where to lookup classes as a list of URLClassPath
Now when the code goes on and it tries to parse these paths, it calls sun.net.www.ParseUtil.decode()
When I put a breakpoint in here, I can see that the path for our class is:
/home/nicolas/Private/%ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2/target/scala-2.12/classes/
so it somehow translated 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 into a percent encoded string : %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2
This in turn creates a CharsetDecoder for UTF-8:
CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
and tries to decode the above url but it fails and that is why the classes can't be loaded.
Now if I go here https://www.branah.com/unicode-converter and tries to convert 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 to a % encoded string, it gives me
%f0%9d%94%98%f0%9d%94%ab%f0%9d%94%a6%f0%9d%94%a0%f0%9d%94%ac%f0%9d%94%a1%f0%9d%94%a2
And if when I debug, I change the url using this encoded string value, ie
/home/nicolas/Private/%f0%9d%94%98%f0%9d%94%ab%f0%9d%94%a6%f0%9d%94%a0%f0%9d%94%ac%f0%9d%94%a1%f0%9d%94%a2/target/scala-2.12/classes/
Then it parses it properly and keeps going.
So I don't know where it gets that %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2
The interesting thing is that if I use the same website and try to convert back %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2 then I also get 𝔘𝔫𝔦𝔠𝔬𝔡𝔢
So I am a bit confused

Why does Files.isHidden(Path) return false for directories on Windows?

From the documentation of Files.isHidden(Path) (emphasis mine):
Tells whether or not a file is considered hidden. The exact definition of hidden is platform or provider dependent. On UNIX for example a file is considered to be hidden if its name begins with a period character ('.'). On Windows a file is considered hidden if it isn't a directory and the DOS hidden attribute is set.
Depending on the implementation this method may require to access the file system to determine if the file is considered hidden.
From this I can understand what the expected behavior is. However, why is this the expected behavior?
The reason I'm wondering is because of the difference in behavior between Files.isHidden, DosFileAttributes.isHidden, and Windows' File Explorer. For instance, I can go into File Explorer and set a directory to be hidden and it will no longer show up (unless I configure it to show hidden items). If I test if said directory is hidden with Java then Files.isHidden returns false and DosFileAttributes.isHidden returns true. You can test this with the following code:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributes;
public class Main {
public static void main(String[] args) throws Exception {
final var directory = Path.of(args[0]).toAbsolutePath().normalize();
final var store = Files.getFileStore(directory);
final var dosAttrs = Files.readAttributes(directory, DosFileAttributes.class);
System.out.println("Directory : " + directory);
System.out.println("FileStore : " + store.name() + " [" + store.type() + "]");
System.out.println("Hidden (Files): " + Files.isHidden(directory));
System.out.println("Hidden (Dos) : " + dosAttrs.isHidden());
}
}
Note: I'm using Windows 10 and OpenJDK 11.0.1. My file system is NTFS.
Running this with:
java Main.java C:\path\to\hidden\directory
I get:
Directory : C:\path\to\hidden\directory
FileStore : OS [NTFS]
Hidden (Files): false
Hidden (Dos) : true
Note: This behavior appears to be part of WindowsFileSystemProvider. The method Files.isHidden(Path) simply forwards the call to the argument's FileSystem's provider. The implementation is basically:
DosFileAttributes attrs = ...; // get attributes
return !attrs.isDirectory() && attrs.isHidden();
I found this (non)-issue (JDK-8170334) where a comment says:
I don't think we have a bug here because the hidden attribute is meaningless on directories.
Yet File Explorer, which is core software on Windows, behaves like the hidden attribute is not meaningless on directories. So again, why does the Java implementation on Windows take into account whether or not the Path points to a directory? Or is Java correct and File Explorer is doing non-standard things?
I'm inclined to think File Explorer is correct because both CMD (via dir) and PowerShell (via Get-ChildItem) won't list hidden directories either; not unless the appropriate options are specified.
I checked documentation for file attributes provided by Microsoft for Windows platform. It says that if attribute FILE_ATTRIBUTE_HIDDEN = 2 (0x2) is set
The file or directory is hidden. It is not included in an ordinary directory listing.
As I can see in the class sun.nio.fs.WindowsConstants there is the same value definition used by DosFileAttributes.isHidden() method - public static final int FILE_ATTRIBUTE_HIDDEN = 0x00000002; which for my understanding should be mapped one to one with the attribute available for Windows, so in general hidden flag for a directory should be working in the same way as for a regular file.
In relation to operating system/file system integration, this behaviour seems to be incorrect.

How to provide the -Dlogback.configurationFile=./logback.xml option in nodejs spawn method

I have angular2-node.js application. I am executing a jar file through the node server.
Jar execution is happening fine but it's using the logback.xml present in the jar file.
Node js code:
app.get('/report/:parameter1/:parameter2', function(req, res) {
var fileName = path.join(__dirname, '..', 'javaFile', 'xyz.jar');
spawn('/usr/bin/java', ['-jar ', fileName, parameter1 , parameter2, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
data = '{response: Success}';
res.status(200).json(data);
res.end();
});
I want to refer the different logback.xml file for jar execution while running the jar from UI. So, i tried the below code:
spawn('/usr/bin/java', ['-jar -Dlogback.configurationFile=./logback.xml', fileName, cacheName , cacheType, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
But, it also didn't work and throw the below error:
Unrecognized option: -jar -Dlogback.configurationFile=./logback.xml
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
I am new to node js. I searched the web but couldn't get an answer.
Is there any way to provide the logback.xml file dynamically in node.js code something like we do in shell script like below:
nohup java -jar -Dlogback.configurationFile=./logback.xml xyz.jar
Can anyone provide any solution for this.
The args arguments is <string[]>, so you should split the multiple args into multiple elements of the array, like you've done for the other arguments. You can check the signature of the method here.
Try,
spawn('/usr/bin/java', ['-jar', '-Dlogback.configurationFile=./logback.xml'], ....

Internal Action was not loaded Error: java.lang.ClassNotFoundException

I am trying to run an implementation a jason code that is using some Internal Actions. The interpreter is showing that it was not possible to find the "java" code of the internal action, as showed:
Server running on http://191.36.8.42:3272
[aslparser] [peleus.asl:29] warning: The internal action class for 'org.soton.peleus.act.plan(Goals)' was not loaded! Error:
java.lang.ClassNotFoundException: org.soton.peleus.act.plan
[aslparser] [peleus.asl:42] warning: The internal action class for 'org.soton.peleus.act.isTrue(H)' was not loaded! Error:
java.lang.ClassNotFoundException: org.soton.peleus.act.isTrue
[peleus] Could not finish intention: intention 1: +des([on(b3,table),on(b2,b3),on(b1,b2)])[source(self)] <- ... org.soton.peleus.act.plan(Goals); !checkGoals(Goals); .print("Goals ",Goals," were satisfied") /
{Goals=[on(b3,table),on(b2,b3),on(b1,b2)]}Trigger: +des([on(b3,table),on(b2,b3),on(b1,b2)])[noenv,code(org.soton.peleus.act.plan([on(b3,table),on(b2,b3),on(b1,b2)])),code_line(29),code_src("peleus.asl"),error(action_failed),error_msg("no environment configured!"),source(self)]
[peleus] Adding belief clear(table)
This mas2j file is as following:
MAS peleus {
infrastructure: Centralised
agents:
peleus;
}
Part of agent code (written by Felipe Meneguzzi) is showed bellow:
//The next line is line 28
+des(Goals) : true
<- org.soton.peleus.act.plan(Goals);
!checkGoals(Goals);
.print("Goals ",Goals," were satisfied").
+!checkGoals([]) : true <- true.
//The next line is line 40
+!checkGoals([H|T]) : true
<- .print("Checking ", H);
org.soton.peleus.act.isTrue(H);
!checkGoals(T).
I guess it is about the folder structure, how to set up Jason to search for java files in specific locations?
The folders structure is like this:
Peleus\src\org\soton\peleus for java files
Peleus\examples for mas2j and asl tested project
It all depends on how you are executing the application.
If you are using java, the CLASSPATH should be defined to include the missing classes.
if you are using jason script (that uses Ant), the .mas2j file should include the class path as well.
More on that in the FAQ. Notice that CLASSPATH is where .class files are found, not .java source code files. The error regards a missing class, not a missing source code.

MissingResourceException: Can't find bundle for base name sun.util.logging.resources.logging, locale en_US

I am getting,
Caused by java.lang.InternalError: java.util.MissingResourceException: Can't find bundle for base name sun.util.logging.resources.logging, locale en_US
in my application from firebase crash report.
Other details
Manufacturer: HTC
Model: HTC 10
Android API: 24
Here is the stack trace
java.util.logging.Logger$1.run (Logger.java:1385)
java.util.logging.Logger$1.run (Logger.java:1379)
java.security.AccessController.doPrivileged (AccessController.java:41)
java.util.logging.Logger.findSystemResourceBundle (Logger.java:1378)
java.util.logging.Logger.findResourceBundle (Logger.java:1425)
java.util.logging.Logger.setupResourceInfo (Logger.java:1523)
java.util.logging.Logger.<init> (Logger.java:266)
java.util.logging.Logger.<init> (Logger.java:261)
java.util.logging.LogManager$SystemLoggerContext.demandLogger (LogManager.java:734)
java.util.logging.LogManager.demandSystemLogger (LogManager.java:399)
java.util.logging.Logger.getPlatformLogger (Logger.java:474)
java.util.logging.LoggingProxyImpl.getLogger (LoggingProxyImpl.java:41)
sun.util.logging.LoggingSupport.getLogger (LoggingSupport.java:100)
sun.util.logging.PlatformLogger$JavaLoggerProxy.<init> (PlatformLogger.java:636)
sun.util.logging.PlatformLogger$JavaLoggerProxy.<init> (PlatformLogger.java:631)
sun.util.logging.PlatformLogger.<init> (PlatformLogger.java:246)
sun.util.logging.PlatformLogger.getLogger (PlatformLogger.java:205)
java.net.CookieManager.put (CookieManager.java:262)
okhttp3.JavaNetCookieJar.saveFromResponse (JavaNetCookieJar.java:47)
okhttp3.internal.http.HttpHeaders.receiveHeaders (HttpHeaders.java:182)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:95)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java:185)
okhttp3.RealCall.execute (RealCall.java:69)
Here is the relevant Logger code
private static ResourceBundle findSystemResourceBundle(final Locale var0) {
return (ResourceBundle)AccessController.doPrivileged(new PrivilegedAction() {
public ResourceBundle run() {
try {
return ResourceBundle.getBundle("sun.util.logging.resources.logging", var0, ClassLoader.getSystemClassLoader());
} catch (MissingResourceException var2) {
throw new InternalError(var2.toString());
}
}
});
}
I have got crash report for locale en_AU too.
Since the crashing code is not controlled by me, how do I prevent this crash?
Analysis:
MissingResourceException: Can't find bundle for base name
sun.util.logging.resources.logging, locale en_US
The system generates candidate bundle names
sun/util/logging/resources/logging_en_US
sun/util/logging/resources/logging_en
sun/util/logging/resources/logging
For each candidate bundle name, it attempts to load a resource bundle:
First, it attempts to load a class using the generated class name.
If such a class can be found and loaded using the specified class
loader, is assignment compatible with ResourceBundle, is accessible
from ResourceBundle, and can be instantiated, getBundle creates a new
instance of this class and uses it as the result resource bundle.
Otherwise, getBundle attempts to locate a property resource file using the generated properties file name.
It generates a path name
from the candidate bundle name by replacing all "." characters with
"/" and appending the string ".properties". It attempts to find a
"resource" with this name using
java.lang.ClassLoader.getResource(java.lang.String). (Note that a
"resource" in the sense of getResource has nothing to do with the
contents of a resource bundle, it is just a container of data, such as
a file.) If it finds a "resource", it attempts to create a new
PropertyResourceBundle instance from its contents. If successful, this
instance becomes the result resource bundle.
Have a more detailed look here how a ressource is resolved.
Since the system is looking in the classpath for any of these files in descending order (usually there are no _en*.properties)
sun/util/logging/resources/logging_en_US.properties
sun/util/logging/resources/logging_en.properties
sun/util/logging/resources/logging.properties
it would be a workaround to add such a file (or a class as mentioned above) to your app.
The content of the property files looks like this:
# Localizations for Level names. For the US locale
# these are the same as the non-localized level name.
# The following ALL CAPS words should be translated.
ALL=All
# The following ALL CAPS words should be translated.
SEVERE=Severe
# The following ALL CAPS words should be translated.
WARNING=Warning
# The following ALL CAPS words should be translated.
INFO=Info
# The following ALL CAPS words should be translated.
CONFIG= Config
# The following ALL CAPS words should be translated.
FINE=Fine
# The following ALL CAPS words should be translated.
FINER=Finer
# The following ALL CAPS words should be translated.
FINEST=Finest
# The following ALL CAPS words should be translated.
OFF=Off
If it works a resource file as suggested by the other answer is a better option, but if that doesn't pan out I have another option. Assuming that you don't need the logging you should be able to disable it like this:
try {
Class<?> cls = Class.forName("sun.util.logging.PlatformLogger");
Field field = cls.getDeclaredField("loggingEnabled");
field.setAccessible(true);
field.set(null, Boolean.FALSE);
} catch(Exception e) {
// Failed
}
If you do that early the PlatformLogger should construct a DefaultLoggerProxy instead of a JavaLoggerProxy, so the broken code never runs. This is ugly as it depends on internal implementation details and as it disables logging, but perhaps it works for your application?

Categories