Alloy API resulting in java.lang.UnsatisfiedLinkError - java

I'm currently using the Alloy Analyzer API to build a program, and getting some peculiar behavior. Specifically, if I open a file and parse it (using CompUtil.parseEverything), then make a new Command and call TranslateAlloyToKodkod.execute_command on the parsed file and newly created command using MiniSat with UNSAT core, it runs fine. However, later in execution, my program parses a second input file (also using CompUtil.parseEverything), gets another world, makes a new command, and then I try to call TranslateAlloyToKodkod.execute_command again, it throws the following error:
ERROR: class edu.mit.csail.sdg.alloy4.ErrorFatal: The required JNI library cannot be found:
java.lang.UnsatisfiedLinkError: no minisatproverx5 in java.library.path
edu.mit.csail.sdg.alloy4compiler.translator.TranslateAlloyToKodkod.execute_command(TranslateAlloyToKodkod.java:390)
Does anyone have any idea why this is thrown the second time, but not the first?
To summarize, I have something similar to the following:
Module someWorld = CompUtil.parseEverything_fromFile(rep, null, "someFile.als");
//For the following, "sig" is a sig in someWorld.getAllReachableSigs();
Command command = sig.not();
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.MiniSatProverJNI;
A4Solution ans =
TranslateAlloyToKodkod.execute_command(rep, someWorld, command, options);
//No thrown error
Module someOtherWorld = CompUtil.parseEverything_fromFile(rep, null, "someOtherFile.als");
//For the following, "sig" is a sig in someOtherWorld.getAllReachableSigs();
Command commandTwo = sig.not();
A4Solution ansTwo =
TranslateAlloyToKodkod.execute_command(rep, someOtherWorld, commandTwo, options);
//Thrown error above. Why?

I tried to reproduce this behavior, but I couldn't. If I don't add MiniSat binaries to the LD_LIBRARY_PATH environment variable, I get the exception you mentioned the very first time I invoke execute_command. After configuring LD_LIBRARY_PATH, the exception doesn't happen.
To configure LD_LIBRARY_PATH:
(1) if using Eclipse, you can right-click on one of your source folders, choose Build Path -> Configure Build Path, then on the "Source" tab make sure that "Native library location" points to a folder in which MiniSat binaries reside.
(2) if running from the shell, just add the path to a folder with MiniSat binaries to LD_LIBRARY_PATH, e.g., something like export LD_LIBRARY_PATH=alloy/extra/x86-linux:$LD_LIBRARY_PATH.
Here is the exact code that I was running, and everything worked
public static void main(String[] args) throws Exception {
A4Reporter rep = new A4Reporter();
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.MiniSatProverJNI;
Module someWorld = CompUtil.parseEverything_fromFile(rep, null, "someFile.als");
Command command = someWorld.getAllCommands().get(0);
A4Solution ans = TranslateAlloyToKodkod.execute_command(rep, someWorld.getAllReachableSigs(), command, options);
System.out.println(ans);
Module someOtherWorld = CompUtil.parseEverything_fromFile(rep, null, "someOtherFile.als");
Command commandTwo = someOtherWorld.getAllCommands().get(0);
A4Solution ansTwo = TranslateAlloyToKodkod.execute_command(rep, someOtherWorld.getAllReachableSigs(), commandTwo, options);
System.out.println(ansTwo);
}
with "someFile.als" being
sig A {}
run { some A } for 4
and "someOtherFile.als"
sig A {}
run { no A } for 4

I use alloy4.2.jar as a library in my eclipse plugin project.
A4Reporter rep = new A4Reporter();
Module world = CompUtil.parseEverything_fromFile(rep, null, "civi.als");
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.SAT4J;
options.skolemDepth = 1;
When I use SAT4J, the default solver, the problem mentioned here will not show up. But another exception comes out. The reason is that my civi.als file need Integer model, which located in alloy4.2.jar under the folder /models/util/. But when I run the application, it tries to find the file util/Integer.als directly. That causes the exception. Is it possible to fix that problem?
Besides, I also tried to put the alloy4.2.jar in eclipse plugin project and run my application as an eclipse application (running my application as a plugin). With the default solver, the application has no problem at all. But when I switch to MiniSatProverJNI, the problem mentioned here comes out (I have set the alloy4.2.jar as classpath).

Related

How to fix a NullPointerException error when running a program with becker.jar as an external jar?

I am setting up a program for a class I am taking. All the code has been provided and I have configured everything into Eclipse. I also had to use becker.jar as an external jar file in the Libraries -> Classpath. When I run the program an error occurs about a NullPointerException.
I already tried changing the becker.jar into the modulepath instead of classpath but then Eclipse cannot find the file. I also tried reinstalling becker.jar as well as redoing the entire setup for the project.
Here is the code I am trying to set up:
import becker.robots.*;
/*
Starting Template:
This file was created in order to provide you with a pre made
'starter' program
*/
public class Starting_Template extends Object {
public static void main(String[] args) {
City toronto = new City();
Robot jo = new Robot(toronto, 3, 0, Direction.EAST, 0);
new Thing(toronto, 3, 2);
jo.move();
jo.turnLeft();
}
}
When running this error shows up:
Exception in thread "main" java.lang.NullPointerException
at java.desktop/sun.font.FontDesignMetrics.getDefaultFrc(FontDesignMetrics.java:158)
at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:279)
at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1183)
at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1646)
at java.desktop/javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(BasicLabelUI.java:245)
at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1680)
at java.desktop/javax.swing.JSlider.updateLabelUIs(JSlider.java:853)
at java.desktop/javax.swing.JSlider.setLabelTable(JSlider.java:824)
at becker.robots.x.<init>(SourceFile:32)
at becker.robots.RobotUIComponents.<init>(SourceFile:87)
at becker.robots.RobotUIComponents.<init>(SourceFile:110)
at becker.robots.City.a(SourceFile:228)
at becker.robots.City.<init>(SourceFile:97)
at becker.robots.City.<init>(SourceFile:47)
at Starting_Template.main(Starting_Template.java:10)
This is a bug. See for example https://issues.jboss.org/browse/PLANNER-255?_sscc=t - the becker.jar has nothing to do with it.

Java eclipse runs older version of my code

A weird issue suddenly appeared. I cannot execute my current code. Eclipse executes older version of my code.
I have read some solutions about this, but none of them worked on my project.
I did Project > Clean and Project > Build Automatically
, change content of my project as System.out.println("hello"); and Eclipse still executes older version of my code.
This is the code that needs to be executed.
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
HTable hTable = new HTable(config, "users");
Put p = new Put(Bytes.toBytes("2"));
p.add(Bytes.toBytes("username"),
Bytes.toBytes("usr"),Bytes.toBytes("dino"));
p.add(Bytes.toBytes("password"),
Bytes.toBytes("pass"),Bytes.toBytes("123"));
hTable.put(p);
System.out.println("data inserted");
hTable.close();
}
And this is the older version of my code:
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config, "myLittleHBaseTable");
Put p = new Put(Bytes.toBytes("myLittleRow"));
p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
Bytes.toBytes("Some Value"));
table.put(p);
Get g = new Get(Bytes.toBytes("myLittleRow"));
Result r = table.get(g);
byte[] value = r.getValue(Bytes.toBytes("myLittleFamily"), Bytes
.toBytes("someQualifier"));
}
I found a solution which says if there is a syntax error, eclipse runs old code. I removed all my codes, and wrote System.out.println("hello"); and it still keeps executing older version of my code.
Second solution did not help me either. I have only one workspace and I currently am using it.
If you are running your code on any server then there might be a jar or war file that is being used for deployment. But if it is a simple hello world program then try re importing your project then rebuild it.
Since I am using Play Framework, I just went to path of my project, and wrote "play eclipsify" and the current code works.
Since you're using Play Framework (as mentioned in your own answer), you should try:
activator clean, then
activator compile to rebuild.
Replace activator with play if you're not using TypeSafe Activator.
This may be a more direct approach than re-eclipsifying your project.

java.lang.IllegalAccessError using org.apache.commons.cli when calling CommandLineParser#parser

I'm trying to use the Apache Commons CLI library to parse command line options in an Eclipse project, roughly following the examples in their Usage Scenarios
I added the commons-cli-1.3.1 folder to the lib folder in the root of the Eclipse project.
I added this to my imports:
import org.apache.commons.cli.*;
And this to the top of my main:
Options options = new Options();
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse( options, args);
} catch ( ParseException e1 ) {
System.err.println( "Unable to parse command-line options: "+e1.getMessage() );
e1.printStackTrace();
}
It compiles without error, but when it runs the parser.parse call generates this error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.commons.cli.Options.getOptionGroups()Ljava/util/Collection; from class org.apache.commons.cli.DefaultParser
I am not using any class loaders at this point.
What does this error mean? How can I resolve the error and parse the arguments?
This is the most probably a dependency problem.
It happens when you compile your code agains one version of the library (1.3.1 in your case) and then run with the older version of this library in your classpath.
I came across exactly this problem today when I had dependecy on commons-cli-1.3.1, but I had commons-cli-1.2 in my classpath (because I used yarn jar to launch my application)
What you should do?
You can just try downgrading to 1.2 as suggested above (this helped me)
Review your classpath and search for another version of commons-cli
What does your exception message really mean?
It means that some code at runtime tries to call some method which it has no right to call. For example, this could be trying to call a private method. Usually this is caught during compilation.
But if, for example, your code tries to call some function which is public in 1.3.1, but was private in 1.2. And if you compiled agains 1.3.1 but trying to launch with 1.2 in the classpath you will get that kind of error.
Hope it is clear.
I am using the commons-cli to handle the command line parameters of my game OpenPatrician. Basically there are three parts to it. The definition of the allowed command line arguments:
Options opts = new Options();
opts.addOption(HELP_OPTION, "help", false, "Display help");
opts.addOption(OptionBuilder.withLongOpt(VERSION_OPTION)
.withDescription("Version of this application")
.create());
opts.addOption(FULLSCREEN_MODE, "fullscreen", false, "fullscreen mode");
opts.addOption(OptionBuilder.withArgName(WINDOWED_MODE)
.withLongOpt("windowed")
.hasOptionalArgs(1)
.withArgName("widthxheight")
.withDescription("Windowed mode with optional definition of window size like: 1280x780")
.create());
opts.addOption(GAME_LOCALE, "lang", true, "Specify the locale to use");
opts.addOption(CLIENT_OPTION, "client", false, "Start application in client mode. Currently unused. Either client or server must be specified");
opts.addOption(SERVER_OPTION, "server", false, "Start application in server mode. Currently unused. Either client or server must be specified");
Providing a help message with all the possible parameters:
public void printHelp(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "OpenPatrician", options );
}
And of course the parsing of the arguments:
public CommandLine parseCommandLine(Options options, String[] args) {
try {
// parse the command line arguments
CommandLineParser parser = new PosixParser();
return parser.parse( options, args );
}
catch( ParseException exp ) {
printHelp(options);
throw new IllegalArgumentException("Parsing of command line arguments failed", exp);
}
}
Note that I am using a PosixPaser here and not the default parser. So that might have different behaviour.

Node JS Trireme include module

I'm running Node JS with https://github.com/apigee/trireme from Java, inside the JVM. I have a directory that looks as following:
node/
-test_file.js
-test_somemodule.js
-somemodule/
-somemodule/index.js
-somemodule/...
I have no problem running the test_file.js using this code:
#Test
public void shouldRunTestScript() {
try {
NodeEnvironment env = new NodeEnvironment();
// Pass in the script file name, a File pointing to the actual script, and an Object[] containg "argv"
NodeScript script = env.createScript("my-test-script.js",
new File(Settings.getInstance().getNodeDir() + "/my-test-script.js"), null);
// Wait for the script to complete
ScriptStatus status = script.execute().get();
// Check the exit code
assertTrue("Exit code was not 77.", status.getExitCode() == 77);
} catch (NodeException | InterruptedException | ExecutionException ex) {
Logger.getLogger(TriremeTest.class.getName()).log(Level.SEVERE, null, ex);
fail("Trireme triggered an exception: " + ex.getMessage());
}
}
In the file test_somemodule.js I include the index.js.
require('somemodule/index.js');
When I try to run that file, it can't find the file in the require.
I have no knowledge about Node JS, so I'm not familiar with the module loading. I already tried setting NODE_PATH, only to get
Error: Cannot find module 'request'
It seems like I can't obtain the NODE_PATH from Trireme, and if I overwrite it, Trireme fails to run. I'm out of ideas on how I could get an Node JS module loaded in Trimere. Any help appreciated.
Edit: I changed the require to ('./somemodule/index.js'), which works. So setting the NODE_PATH would have done the job too. I just found out the error came from an missing dependency.
"dependencies": {
"request": "^2.49.0",
"tough-cookie": "^0.12.1"
},
I figured out the best way to deal with it is installing Node JS + npm, and invoking npm install some_module in the node/ folder. It automatically downloads some_module and all of its dependencies into my node/ folder.
No more require errors.
I did not specify that the file was in the working directory.
require('./somemodule/index.js');
instead of
require('somemodule/index.js');
did the job. Another possiblity is to set the NODE_PATH environment variable to the node/ folder, so you can require without ./.
I also figured out that the best way to obtain modules is by installing them with npm instead of downloading them from git, because the latter does not download any dependencies.

How to make an Eclipse debug launcher that launches a Class

I'm trying to make an Eclipse launch configuration that I can launch programmatically, kind of building a custom debugger if you like.
I've already got an org.eclipse.debug.core.launchConfigurationTypes extension, as well as .core.launchDelegates, .ui.launchConfigurationTabGroups and .core.sourcePathComputers extensions.
I've got a button that executes the following code:
ILaunchManager mgr = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType lct = mgr.getLaunchConfigurationType(IOpcodeConstants.LAUNCH_CFG_TYPE);
ILaunchConfiguration[] lcs = mgr.getLaunchConfigurations(lct);
for (int i = 0; i < lcs.length; ++i) {
if (lcs[i].getName().equals("Opcode")) {
lcs[i].delete();
break;
}
}
ILaunchConfigurationWorkingCopy wc = lct.newInstance(null, "Opcode");
Set<String> modes = new HashSet<String>();
modes.add(ILaunchManager.DEBUG_MODE);
wc.setModes(modes);
wc.setPreferredLaunchDelegate(modes, "nz.net.fantail.studio.OpcodeLaunchDelegate");
ILaunchConfiguration lc = wc.doSave();
lc.launch(ILaunchManager.DEBUG_MODE, null);
My launch delegate has the following code:
#Override
public void launch(ILaunchConfiguration configuration, String mode,
ILaunch launch, IProgressMonitor monitor) throws CoreException {
ManagementClient client = new ManagementClient("localhost", 6961);
if (mode.equals(ILaunchManager.DEBUG_MODE)) {
IDebugTarget target = new OpcodeDebugTarget(launch, client);
launch.addDebugTarget(target);
}
}
Everything works perfectly fine until get tries to load the ManagementClient class and throws a NoSuchClassDefException. I suspect this is because it launches in a separate environment from the actual application and as such doesn't have the .jar with the class in its classpath.
Does anyone know how to get around this issue? Cheers!
What class is it not finding, the ManagementClient or something else? Maybe in your launch configuration you need to set the target classpath yourself.
// customize the classpath
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classPathList);
Here are some other settings that may be useful:
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
projectName);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
targetMainClass);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
programArgs);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, jvmArgs);
Why do you need the button that launches the configuration? If you extend the extension points you mentioned your launch configuration should appear in the debug menu of eclipse ... no need for a seperate button!?
Appart from this I would look after the dependencies of the plugin that contains "ManagementClient". The "NoSuchClassDefException" most often is a result of wrong dependency definitions (maybe the order of the dependencies is wrong [core plugins before ui plugins] ... or your class isn't in an plugin altogether?).

Categories

Resources