main class in fat jar fails when using static factory method, but works fine in IDE - static

I have a small project that i'm using shadowJar to create a fatjar to run from command line..
the main class entry point gets ref to class in the project using static factory method like this
static void main (args){
MessageSystemClient mclient = MessagePlatformFactoryProducer.getFactory().getMessagePlatformInstance("WLS")
println "howdi "....
}
when i run the fat jar at the command line i get an error like so
PS D:\Intellij-projects\message-platform-client\build\libs> java -jar message-platform-client-1.0-SNAPSHOT.jar --send "hello" -r
Exception in thread "main" java.io.FileNotFoundException: file:\D:\Intellij-projects\message-platform-client\build\libs\message-platform-client-1.0-SNAPSHOT.jar!\ApplicationConfig.groovy (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:77)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1741)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.getText(ResourceGroovyMethods.java:592)
at org.codehaus.groovy.runtime.dgm$1013.doMethodInvoke(Unknown Source)
at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.doMethodInvoke(GeneratedMetaMethod.java:83)
at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:63)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:298)
at com.softwood.implementation.MessagePlatformFactory.getMessagePlatformInstance(MessagePlatformFactory.groovy:29)
at com.softwood.client.AbstractMessagePlatformFactory$getMessagePlatformInstance.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
at com.softwood.cli.Launcher.main(Launcher.groovy:40)
however if i comment out the static factory call - and regenerate then the fat jar runs ok and prints the howdi to the console.
if i uncomment the factory lookup again, and just run the Launcher class in the IDE the code works fine with no problems
so why is it failing as a fatjar, but works as ordinary project?. secondly assuming its something to do with fatjar zip approach - how do i overcome this ?
I had a earlier version of the project that just directly called static methods on project classes, and that works fine as a fatjar - so the problem is with the static factory behavior inside a fatjar .
i tried to load the classes using
Launcher.getClass().getClassLoader().loadClass ("<various factory classes etc>")
the classes would load ok - but the factory call itself still breaks as shown above in stack trace
Can any one help me over this problem please?

Found it - in my factory methods I was doing a call like this to load a configFile:
File configFile = new File(classLoader.getResource("ApplicationConfig.groovy")?.getFile())
However as per related link, this won't work in a fat jar as there's no full file system in the jar.
What you have to do is use line like:
InputStream configStream = classLoader.getResourceAsStream("ApplicationConfig.groovy")
as this will work in the jar, and test if stream is null (can't find your resource in the fatjar) or you get the stream itself which you can read the text from the file.
Took a while, but now back on I think.

Related

OptaPlanner benchmarking won't run

I'm trying to run an OptaPlanner benchmark with the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<plannerBenchmark xmlns="https://www.optaplanner.org/xsd/benchmark" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.optaplanner.org/xsd/benchmark https://www.optaplanner.org/xsd/benchmark/benchmark.xsd">
<benchmarkDirectory>data/acme</benchmarkDirectory>
<inheritedSolverBenchmark>
<solver>
<solutionClass>org.acme.domain.Main</solutionClass>
<entityClass>org.acme.domain.Standstill</entityClass>
<entityClass>org.acme.domain.Visit</entityClass>
<scoreDirectorFactory>
<constraintProviderClass>org.acme.solver.MainConstraintProvider</constraintProviderClass>
<initializingScoreTrend>ONLY_DOWN</initializingScoreTrend>
</scoreDirectorFactory>
<termination>
<secondsSpentLimit>30</secondsSpentLimit>
</termination>
</solver>
<problemBenchmarks>
<solutionFileIOClass>org.acme.bootstrap.AcmeSolutionIO</solutionFileIOClass>
<inputSolutionFile>data/latest.xml</inputSolutionFile>
</problemBenchmarks>
</inheritedSolverBenchmark>
<solverBenchmark>
<solver>
<constructionHeuristic>
<constructionHeuristicType>FIRST_FIT</constructionHeuristicType>
</constructionHeuristic>
<localSearch>
<localSearchType>LATE_ACCEPTANCE</localSearchType>
</localSearch>
</solver>
</solverBenchmark>
</plannerBenchmark>
However, I get this error message:
Exception in thread "main" org.optaplanner.benchmark.api.PlannerBenchmarkException: Benchmarking failed: failureCount (1). The exception of the firstFailureSingleBenchmarkRunner (blankData_Config_0_0) is chained.
at org.optaplanner.benchmark.impl.DefaultPlannerBenchmark.benchmarkingEnded(DefaultPlannerBenchmark.java:326)
at org.optaplanner.benchmark.impl.DefaultPlannerBenchmark.benchmark(DefaultPlannerBenchmark.java:100)
at org.optaplanner.benchmark.impl.DefaultPlannerBenchmark.benchmarkAndShowReportInBrowser(DefaultPlannerBenchmark.java:424)
at org.acme.bootstrap.BenchmarkApp.main(BenchmarkApp.kt:29)
Caused by: java.lang.IllegalStateException: The class (class java.lang.Long) should have a no-arg constructor to create a planning clone.
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner.lambda$retrieveCachedConstructor$0(FieldAccessingSolutionCloner.java:90)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
at org.optaplanner.core.impl.domain.common.ConcurrentMemoization.computeIfAbsent(ConcurrentMemoization.java:44)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner.retrieveCachedConstructor(FieldAccessingSolutionCloner.java:85)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner$FieldAccessingSolutionClonerRun.constructClone(FieldAccessingSolutionCloner.java:158)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner$FieldAccessingSolutionClonerRun.clone(FieldAccessingSolutionCloner.java:150)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner$FieldAccessingSolutionClonerRun.process(FieldAccessingSolutionCloner.java:207)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner$FieldAccessingSolutionClonerRun.processQueue(FieldAccessingSolutionCloner.java:194)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner$FieldAccessingSolutionClonerRun.cloneSolution(FieldAccessingSolutionCloner.java:136)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner.cloneSolution(FieldAccessingSolutionCloner.java:73)
at org.optaplanner.core.impl.score.director.AbstractScoreDirector.cloneSolution(AbstractScoreDirector.java:257)
at org.optaplanner.core.impl.score.director.AbstractScoreDirector.cloneWorkingSolution(AbstractScoreDirector.java:250)
at org.optaplanner.core.impl.solver.recaller.BestSolutionRecaller.updateBestSolutionAndFire(BestSolutionRecaller.java:129)
at org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase.phaseEnded(DefaultConstructionHeuristicPhase.java:146)
at org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase.solve(DefaultConstructionHeuristicPhase.java:98)
at org.optaplanner.core.impl.solver.AbstractSolver.runPhases(AbstractSolver.java:99)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:192)
at org.optaplanner.benchmark.impl.SubSingleBenchmarkRunner.call(SubSingleBenchmarkRunner.java:122)
at org.optaplanner.benchmark.impl.SubSingleBenchmarkRunner.call(SubSingleBenchmarkRunner.java:42)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NoSuchMethodException: java.lang.Long.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3585)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
at org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner.lambda$retrieveCachedConstructor$0(FieldAccessingSolutionCloner.java:88)
... 22 more
I've tried many other benchmark configs including with <solverBenchmarkBluePrintType>CONSTRUCTION_HEURISTIC_WITH_AND_WITHOUT_LOCAL_SEARCH</solverBenchmarkBluePrintType> but none of them work either.
I had it working previously, but I went back to it after a number of changes and it no longer works.
I'm using a SimpleLongScore now instead of a SimpleScore and I'm wondering if that has anything to do with the error since before it wasn't throwing this exception.
Any ideas?
Edit #1:
I wish I could provide more code, but I really don't know what code to include.
The bizarre thing is that OptaPlanner runs just fine through mvn compile quarkus:dev, but when I run my benchmark app, from the main entry, it gives the errors as mentioned before, java.lang.IllegalStateException: The class (class java.lang.Long) should have a no-arg constructor to create a planning clone.
Here's my main:
fun main(args: Array<String>) {
val plannerBenchmarkFactory:PlannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource("benchmarkBluePrintConfig.xml")
val plannerBenchmark: PlannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark()
plannerBenchmark.benchmarkAndShowReportInBrowser()
}
The solverConfig.xml can also be the same for both, but only the benchmark app throws the error. It's pretty odd to me.
I figured it out.
In my Visit class I was using a #CustomShadowVariable:
#DeepPlanningClone
#CustomShadowVariable(
variableListenerClass = ArrivalTimeUpdatingVariableListener::class,
sources = [PlanningVariableReference(variableName = "previousStandstill")]
)
var arrivalTime: Long? = null
Removing #DeepPlanningClone solved the problem.
I have no idea why it runs fine when the SolverManager is #Inject'd as opposed to throwing the error when SolverConfig/SolverManager is created through the API (as it is in my Benchmark App).

What are the EJB Remote Interfaces in the project when createEJBStubs.bat is run?

I understand that createEJBstubs are necessary to create the stubs whenever it is accessed externally via. An Java Client (http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/rejb_3stubscmd2.html?lang=ko). And also, that the stubs that are created will be for the EJB3 interfaces that are available within the module. But how do they detect if a given interface/bean is of type EJB 3 and not 2.*. From the issue the I have faced below, it is not clear to me as to how this detection is done. Please help me to understand this so that I can resolve the issue that I am facing (below)-
DETAILED EXPLANATION OF MY ISSUE:
When I run createEJBStubs.bat C:\1\DEN\proj\target\proj.jar -updatefile C:\1\DEN\proj\target\proj.jar -verbose
I get the following error -
CNTR9241I: The C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar Java archive (JAR) file has no level 3.0 enterprise beans with remote interfaces.
And when I run the same command using -verbose option, I get the following error -
createWarProcessingRootDir: enter
Root directory for war processing: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622
War explosion root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls
War primary input root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\primary
War primary classpath root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\primary\classPth
War primary workspace root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\primary\wrkSpace
War alternate workspace root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\alternate\wrkSpace
War alternate output file: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\alternateOutputWar.war
War embedded in ear root: C:\Users\w723521\AppData\Local\Temp\_tempWar_1096410607889881622\warExpls\embInEar
createWarProcessingRootDir: exit
Dumping input parameters:
parameter 1 = C:\1\DEN\proj\target\proj-5.DEV-SNA
PSHOT.jar
parameter 2 = -updatefile
parameter 3 = C:\1\DEN\proj\target\proj-5.DEV-SNA
PSHOT.jar
parameter 4 = -trace
Processing the C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar input file.
checkEJBVersion - jar name is C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar
The output file name is C:\Users\w723521\AppData\Local\Temp\ejb3093395338317385883.jar
copyArchiveEntriesAndStubs(null,C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar,C:\Users\w723521\AppData\Local\Temp\ejb3093395338317385883.jar,false)
Main output archive file (no pre-existing stubs) is C:\Users\w723521\AppData\Local\Temp\ejb3093395338317385883.jar
Preexisting stubs archive file is ejb5694574801473018226.jar
Writing non-stub entry MANIFEST.MF
Writing non-stub entry TestLocal.class
Writing non-stub entry TestRemote.class
Classloader updated for -cp null
Classloader updated for jar C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar
getMetaData - entry
metadataComplete setting is false
findRemoteInterfaces
List of interfaces are: null
Number of pre-existing stubs = 0
CNTR9241I: The C:\1\DEN\proj\target\proj-5.DEV-SNAPSHOT.jar Java archive (JAR) file has no level 3.0 enterprise beans with remote interfaces.
Starting process of deleting workspace files...
Done with process of deleting workspace files...
Command Successful
I am not able to understand why it says that no local and remote interfaces are of EJB3 even though the bean classes created using EJB3 annotations
#Remote
public interface TestRemote {
public void test();
}
#Remote
public interface TestRemote {
public void test();
}
How does the script detect if a given interface is EJB3 or not? (based on the output that I have obtained it is not as clear as I expected it to be)
proj-5.DEV-SNAPSHOT.jar would need to somehow specify both an EJB and its remote interface. For example, if ejb-jar.xml is used, a <session> with a <remote>pkg.TestRemote</remote>. For another example, if some class in the JAR is annotated with #Stateless and #Remote(TestRemote.class).

Can't Get GAE + GWT + Objectify to Work

As the title says, I'm trying to create a GAE + GWT project using Objectify but I can't even get it off the ground. I'm sure I'm missing something simple but doesn't seem to be working.
Here is what I've done so far:
Create a new project and added guava-17.0.jar, guava-gwt-17.0.jar, objectify-5.0.3.jar, and objectify-gwt-1.1jar to my WEB-INF\lib folder. These are all the latest versions of these jars.
Run the application. Send a simple RPC command, server responds, and client successfully receives response (onSuccess() is called).
Add the line <inherits name="com.googlecode.objectify.Objectify" /> to my gwt.xml file per Objectify-GWT's website which is supposed to enable Objectify in GWT.
Run the application. The application starts, same RPC command is sent, server receives and responds, but the client says the command was a failure (onFailure() is called).
I am using the boiler-plate code that is pre-populated when first create a new web application. For reference, here is the RPC command:
private void sendNameToServer() {
// First, we validate the input.
errorLabel.setText("");
String textToServer = nameField.getText();
if (!FieldVerifier.isValidName(textToServer)) {
errorLabel.setText("Please enter at least four characters");
return;
}
// Then, we send the input to the server.
sendButton.setEnabled(false);
textToServerLabel.setText(textToServer);
serverResponseLabel.setText("");
greetingService.greetServer(textToServer,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
dialogBox
.setText("Remote Procedure Call - Failure");
serverResponseLabel
.addStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(SERVER_ERROR);
dialogBox.center();
closeButton.setFocus(true);
}
public void onSuccess(String result) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel
.removeStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(result);
dialogBox.center();
closeButton.setFocus(true);
}
});
}
This is the error I receive after I try to make the RPC call:
[DEBUG] [my_app] - Validating units:
[INFO] [my_app] - Module my_app has been loaded
[ERROR] [my_app] - Errors in 'com/google/gwt/dev/jjs/SourceOrigin.java'
[ERROR] [my_app] - Line 77: The method synchronizedMap(new LinkedHashMap<SourceOrigin,SourceOrigin>(){}) is undefined for the type Collections
[ERROR] [my_app] - Errors in 'com/google/gwt/dev/util/StringInterner.java'
[ERROR] [my_app] - Line 29: No source code is available for type com.google.gwt.thirdparty.guava.common.collect.Interner<E>; did you forget to inherit a required module?
[ERROR] [my_app] - Line 29: No source code is available for type com.google.gwt.thirdparty.guava.common.collect.Interners; did you forget to inherit a required module?
To me it looks like Objectify is interfering with GWT. I know they're supposed to work together so not sure what I'm doing wrong. Any advice would be appreciated.
Use objectify-gwt 1.2. It's possible that 1.1 has some issues from merging a bad PR.
You can see a sample application that uses objectify-gwt to pass a GeoPt back and forth from the client here: https://github.com/stickfigure/objectify-gwt-test
You should use objectify on the server side before trying to do this kind of stuff. Objectify is a server side peristence technology. Call it in your server code
add try catch in your service methods and print the stack trace of the exception on your server console, if you receive onFailure() on GWT that means there is a failure on the server side. You have to find what is that failure.
Now the second part is an advice:
<inherits name="com.googlecode.objectify.Objectify" />
Is a weired line for me. GWT doesn't have to know about your persistence layer.
Unless it's a revolutionary concept, I would recommend you d'ont use this type of technology that removes your hand from the controle of your db access...

Using Fat-Free PHP for Backbone.js routing with external Model.php files

I'm very new to Fat-Free and Backbone.js. I've been searching and reading articles and searching and reading articles trying to find a way to route to individual PHP files containing the database communications. The code below works, and I can use it, but it seems hackish. Is there a way to call an external PHP file (in the server/models/ directory) and a specific method from the $f3-route(...) line?
<?php
// File: /index.php
define("PATH",1);
$f3 = require('server/fatfree/lib/base.php');
$uri = explode('/', $_SERVER["REQUEST_URI"]);
require_once "server/models/{$uri[PATH]}.php";
$f3->route('GET /hello/#file', 'HelloModel->doSomething');
$f3->route('GET /project/#file', 'ProjectModel->doSomething');
$f3->route('GET /book/#file', 'BookModel->doSomething');
$f3->run();
?>
Thanks a lot for your advice.
You should add the server/models directory to the autoloader of F3 using the autoloader feature.
$f3->set('AUTOLOAD','server/models/');
That way, the required source files of your classes will be loaded on demand. However, note that the files must be named the same as your class, i.e. class Foo has to be defined in foo.php or Foo.php. The case of the filename does not matter.

problem with loading a jar file which has dependency to another jar file

I have a problem while loading my jar file at run time.
My hotel.jar is loaded and a method of it (makeReservation) is invoked using the following code:
File file = new File("c:/ComponentFiles/hotel.jar");
URL jarfile = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/");
URLClassLoader cl = URLClassLoader.newInstance(new URL[]{jarfile});
Class componentClass = cl.loadClass("HotelPackage.HotelMgt");
Object componentObject = componentClass.newInstance();
Method setMethod = componentClass.getDeclaredMethod("makeReservation", null);
setMethod.invoke(componentObject, null);
The problem is in the class HotelPackage.HotelMgt of my jar file , I have a class variable of another class (HotelPackage.Hotel) which is in another jar file.
I tried to open and load the other jar file with the same code as above but I receive the exception that cannot find the class def.:
Exception in thread "main" java.lang.NoClassDefFoundError: BeanPackage/Hotel
what is the solution?
You can specify dependencies between JARs by defining the Class-Path property in the JARs' manifest files. Then the JVM will load the dependency JARs automatically as needed.
More information is here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
Thanks, but I found another solution that really works. Since I know whole the component series that are going to work with each other, I load them all with one class loader instance (array of URLs). then the classloader itself manages the dependencies.

Resources