I am a student who begin to study SHOP2 from China.
My teacher told me to run JSHOP2 in Eclipse.Now I can run original zenotravel problem and generate GUI and plans.Likewise, I want to put other domain and problems to SHOP2 and produce plans.
But the problem is that I don't know how to compile them and My teacher only asked me to run the the main function in Internaldomain but it can't succeed.Follow is the original code:
public static void main(String[] args) throws Exception
{
//compile();
// compile(args);
//-- run the planning algorithm
run(args);
}
This code can run zenotravel.Then I put domain and problems named pfile1 and
tdepots respectively into SHOP2 folder.Change the codes to:
{
compile(domaintdepots);
// compile(args);
//-- run the planning algorithm
run(args);
}
It warns "domainpdfiles cannot be resolved to a variable".
Or
//--compile();
compile(args);
//-- run the planning algorithm
//run(args);
It turns out:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at JSHOP2.InternalDomain.compile(InternalDomain.java:748)
at JSHOP2.InternalDomain.main(InternalDomain.java:720)"
720 is main funcition above.And 748 is compile function:
public static void compile(String[] args) throws Exception
{
//-- The number of solution plans to be returned.
int planNo = -1;
//-- Handle the number of solution plans the user wants to be returned.
if (args.length == 2 || args[0].substring(0, 2).equals("-r")) {
if (args[0].equals("-r"))
planNo = 1;
else if (args[0].equals("-ra"))
planNo = Integer.MAX_VALUE;
else try {
planNo = Integer.parseInt(args[0].substring(2));
} catch (NumberFormatException e) {
}
}
Finally,according to the advice of the friend,I put the two pddls into src folder and use “java Jshop2.InternalDomain domaintdepots”in CMD commad but an error appeared:"the main class Interdomain can't be found or loaded".But I have set the class path accurately and the Zenotravel planning can run.So how
and where can I use the command ?
And what is written in the bracket"compile()" in Eclipse?
I am also not familiar with JAVA so it's better if there is concrete instruction.Thanks a lot.
Please describe what are you trying to build, what is it supposed to do, what is the expected end result.
If you do have a valid PDDL domain and problem file, you could try to load them into the online http://editor.planning.domains/ editor using the File > Load menu. Then press the Solve button and confirm which of the file is the domain and which is problem. If the PDDL model is valid (and the underlying solver can handle the requirements), you will get a plan back.
If you are trying to build a software solution that needs a PDDL-based planning engine as one of its component, perhaps you could use one of the available implementations: https://nergmada.github.io/pddl-reference/guide/whatisplanner.html#list-of-planners
If you are trying to build your own planning engine in Java using the Eclipse IDE, you probably need a Java-based PDDL parser. Here is a tutorial, how to use pddl4j for that purpose:
https://github.com/pellierd/pddl4j/wiki/A-tutorial-to-develop-your-own-planner
If you need to use Jshop2 in particular, it looks from their documentation (http://www.cs.umd.edu/projects/shop/description.html) that you need to indeed compile the domain and problem PDDL into Java code using following commands:
java JSHOP2.InternalDomain domainFileName
java JSHOP2.InternalDomain -r problemFileName
Edited on June 19th
Java package names (e.g. JSHOP2) and class names (InternalDomain) are case sensitive, so make sure you type them as per the documentation. That is probably why you are getting the "main class not found error".
It is difficult to say what the lines numbers 748 and 720 exactly correspond to, because in the GitHub repo https://github.com/mas-group/jshop2/blob/master/src/JSHOP2/InternalDomain.java the code is different from yours. Can you indicate in your questions which lines those are exactly?
The make file shows how to execute an out-of-the-box example in the distribution:
cd examples\blocks
java JSHOP2.InternalDomain blocks
java JSHOP2.InternalDomain -r problem300
Does that work for you?
I have an Eclipse project that contains several source files, with a bunch of different encodings: some files are UTF8, some others are ISO-8859-1, others more are windows-1252.
Moreover, there are files whose encoding is explicit (it can bee seen in each file Properties window) while that of others is Inherited from container.
I need to convert them to UTF8 - and I've already found I can use iconv for that - see my answer here for details -, but since they're more than one thousand, I can't convert them one by one: is there any programmatic way to get the encoding from the IDE or something similar?
I'm on Windows, I may do some shell scripting and / or write auxiliary software.
The charset setting of project file could be found in ${PROJECT_FOLDER}/.settings/org.eclipse.core.resources.prefs
Since there are so many files in your project. Create a simple Eclipse plugin could reduce the effort. Here are the steps:
Select Eclipse menu item New > Project... > Plug-in Project
Accept default values and goto last page, choose Hello, World template.
Open and edit plugin.xml, adding required plugin org.eclipse.core.resources in Dependencies page.
Edit SampleAction.java, refer the sample code below..
Create a Debug Configuration for execute Eclipse Application.
Start the eclipse and import your project.
Select menu item Sample Menu to invoke public void run(IAction action).
Sample code of Eclipse resource API:
//import org.eclipse.core.resources.*;
//import org.eclipse.core.runtime.*;
public void run(IAction action) {
final boolean keepHistory = true;
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("yourProjectName");
project.accept(new IResourceVisitor() {
public boolean visit(IResource resource) {
IFile file = (IFile)resource;
try {
String charset = file.getCharset(true);
if (!"UTF-8".equals(charset)) {
InputStream is = file.getContents();
//convert to UTF-8 and save it
String str = new String(IOUtils.toByteArray(is), charset);
file.setContents(new ByteArrayInputStream(str.getBytes("UTF-8")), true, keepHistory, null);
//remove charset setting
file.setCharset("UTF-8", null);
}
} catch (Throwable e) {
//log error... maybe process later via project.getFile(new Path(projectRelativePath))
Activator.getDefault().getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID,
file.getProjectRelativePath().toString(), e));
}
return false;
}
}, IResource.DEPTH_INFINITE, IResource.FILE);
}
I took a backup of my workspace. Meanwhile I created another workspace in my pc after formatting for temprory purposes. So now I've imported everything from my hard disk to my pc from File -> Import. However nothing seems to be 'resolved'. Heres an example of the code:
package com.sarthak.main;
import java.util.Scanner;
public class Myfirstgame {
public static void main(String[] args) {
System.out.println("Welcome to Momo Land, a place full of horror and utter momoness ");
System.out.println("Enter Your Name: ");
Scanner input = new Scanner(System.in);
String name=input.nextLine();
MyGame game=new MyGame(name);
game.run();
}
}
Things like System.out.println , input game.run etc. can't be resolved for some reason.
Check if your Java libraries are referenced in the properties of your project. In most cases, it is the referencing problem when importing projects.
Create a new project and then just copy the code inside the files..This may solve your problem.
How can I change the current working directory from within a Java program? Everything I've been able to find about the issue claims that you simply can't do it, but I can't believe that that's really the case.
I have a piece of code that opens a file using a hard-coded relative file path from the directory it's normally started in, and I just want to be able to use that code from within a different Java program without having to start it from within a particular directory. It seems like you should just be able to call System.setProperty( "user.dir", "/path/to/dir" ), but as far as I can figure out, calling that line just silently fails and does nothing.
I would understand if Java didn't allow you to do this, if it weren't for the fact that it allows you to get the current working directory, and even allows you to open files using relative file paths....
There is no reliable way to do this in pure Java. Setting the user.dir property via System.setProperty() or java -Duser.dir=... does seem to affect subsequent creations of Files, but not e.g. FileOutputStreams.
The File(String parent, String child) constructor can help if you build up your directory path separately from your file path, allowing easier swapping.
An alternative is to set up a script to run Java from a different directory, or use JNI native code as suggested below.
The relevant OpenJDK bug was closed in 2008 as "will not fix".
If you run your legacy program with ProcessBuilder, you will be able to specify its working directory.
There is a way to do this using the system property "user.dir". The key part to understand is that getAbsoluteFile() must be called (as shown below) or else relative paths will be resolved against the default "user.dir" value.
import java.io.*;
public class FileUtils
{
public static boolean setCurrentDirectory(String directory_name)
{
boolean result = false; // Boolean indicating whether directory was set
File directory; // Desired current working directory
directory = new File(directory_name).getAbsoluteFile();
if (directory.exists() || directory.mkdirs())
{
result = (System.setProperty("user.dir", directory.getAbsolutePath()) != null);
}
return result;
}
public static PrintWriter openOutputFile(String file_name)
{
PrintWriter output = null; // File to open for writing
try
{
output = new PrintWriter(new File(file_name).getAbsoluteFile());
}
catch (Exception exception) {}
return output;
}
public static void main(String[] args) throws Exception
{
FileUtils.openOutputFile("DefaultDirectoryFile.txt");
FileUtils.setCurrentDirectory("NewCurrentDirectory");
FileUtils.openOutputFile("CurrentDirectoryFile.txt");
}
}
It is possible to change the PWD, using JNA/JNI to make calls to libc. The JRuby guys have a handy java library for making POSIX calls called jnr-posix. Here's the maven info
As mentioned you can't change the CWD of the JVM but if you were to launch another process using Runtime.exec() you can use the overloaded method that lets you specify the working directory. This is not really for running your Java program in another directory but for many cases when one needs to launch another program like a Perl script for example, you can specify the working directory of that script while leaving the working dir of the JVM unchanged.
See Runtime.exec javadocs
Specifically,
public Process exec(String[] cmdarray,String[] envp, File dir) throws IOException
where dir is the working directory to run the subprocess in
If I understand correctly, a Java program starts with a copy of the current environment variables. Any changes via System.setProperty(String, String) are modifying the copy, not the original environment variables. Not that this provides a thorough reason as to why Sun chose this behavior, but perhaps it sheds a little light...
The working directory is a operating system feature (set when the process starts).
Why don't you just pass your own System property (-Dsomeprop=/my/path) and use that in your code as the parent of your File:
File f = new File ( System.getProperty("someprop"), myFilename)
The smarter/easier thing to do here is to just change your code so that instead of opening the file assuming that it exists in the current working directory (I assume you are doing something like new File("blah.txt"), just build the path to the file yourself.
Let the user pass in the base directory, read it from a config file, fall back to user.dir if the other properties can't be found, etc. But it's a whole lot easier to improve the logic in your program than it is to change how environment variables work.
I have tried to invoke
String oldDir = System.setProperty("user.dir", currdir.getAbsolutePath());
It seems to work. But
File myFile = new File("localpath.ext");
InputStream openit = new FileInputStream(myFile);
throws a FileNotFoundException though
myFile.getAbsolutePath()
shows the correct path.
I have read this. I think the problem is:
Java knows the current directory with the new setting.
But the file handling is done by the operation system. It does not know the new set current directory, unfortunately.
The solution may be:
File myFile = new File(System.getPropety("user.dir"), "localpath.ext");
It creates a file Object as absolute one with the current directory which is known by the JVM. But that code should be existing in a used class, it needs changing of reused codes.
~~~~JcHartmut
You can use
new File("relative/path").getAbsoluteFile()
after
System.setProperty("user.dir", "/some/directory")
System.setProperty("user.dir", "C:/OtherProject");
File file = new File("data/data.csv").getAbsoluteFile();
System.out.println(file.getPath());
Will print
C:\OtherProject\data\data.csv
You can change the process's actual working directory using JNI or JNA.
With JNI, you can use native functions to set the directory. The POSIX method is chdir(). On Windows, you can use SetCurrentDirectory().
With JNA, you can wrap the native functions in Java binders.
For Windows:
private static interface MyKernel32 extends Library {
public MyKernel32 INSTANCE = (MyKernel32) Native.loadLibrary("Kernel32", MyKernel32.class);
/** BOOL SetCurrentDirectory( LPCTSTR lpPathName ); */
int SetCurrentDirectoryW(char[] pathName);
}
For POSIX systems:
private interface MyCLibrary extends Library {
MyCLibrary INSTANCE = (MyCLibrary) Native.loadLibrary("c", MyCLibrary.class);
/** int chdir(const char *path); */
int chdir( String path );
}
The other possible answer to this question may depend on the reason you are opening the file. Is this a property file or a file that has some configuration related to your application?
If this is the case you may consider trying to load the file through the classpath loader, this way you can load any file Java has access to.
If you run your commands in a shell you can write something like "java -cp" and add any directories you want separated by ":" if java doesnt find something in one directory it will go try and find them in the other directories, that is what I do.
Use FileSystemView
private FileSystemView fileSystemView;
fileSystemView = FileSystemView.getFileSystemView();
currentDirectory = new File(".");
//listing currentDirectory
File[] filesAndDirs = fileSystemView.getFiles(currentDirectory, false);
fileList = new ArrayList<File>();
dirList = new ArrayList<File>();
for (File file : filesAndDirs) {
if (file.isDirectory())
dirList.add(file);
else
fileList.add(file);
}
Collections.sort(dirList);
if (!fileSystemView.isFileSystemRoot(currentDirectory))
dirList.add(0, new File(".."));
Collections.sort(fileList);
//change
currentDirectory = fileSystemView.getParentDirectory(currentDirectory);
I recently changed the build path for a couple of packages in an eclipse project. Ultimately the file structure should have stayed the same however. So what was once the package main.java.edu.umb.cs is now edu.umb.cs. All the links were working fine before, but now I'm getting the error that Tokanagrammar.fxml can't be found. The following is in Tokanagrammar.java (the topmost expanded package in the image below):
..getResource("../../../../resources/fxml/Tokanagrammar.fxml"));
EDIT1: code to read resources in
public void start(Stage primaryStage) {
try {
AnchorPane page = (AnchorPane) FXMLLoader.load(Tokanagrammar.class.getResource("../../../../resources/fxml/Tokanagrammar.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("Tokanagrammar 0.1");
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Tokanagrammar.class.getName()).log(Level.SEVERE, null, ex);
}
}
Again, this worked fine before I took some of the src out of the build path. As far as I am aware, a resource does not have to be in the build path to be able to be accessed. I've had projects that have had an image folder, for instance, outside of the build path without any problems.
So what gives? I can also easily move the .fxml file into the package where Tokanagrammar.java resides and edit the link and it works just fine. However, per the specifications is has to be the way described.
Any help is greatly appreciated.
Rather than relying on (changing) paths relative to your Tokanagrammar class, you should load the resource relative to the root of your classpath. This is especially true since the resource is in an entirely different package from the class (as far as path resolution goes). Try this instead:
AnchorPane page = (AnchorPane) FXMLLoader.load(
Thread.currentThread().getContextClassLoader().
getResource("fxml/Tokanagrammar.fxml"));
Note that this will work as long as all the resources in src/main/resources are being copied to the root of your class path.