Skip to content

Commit

Permalink
consider jars in classpath for jason-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jomifred committed Jun 17, 2024
1 parent 2088396 commit 6000f8c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
29 changes: 18 additions & 11 deletions jason-cli/src/main/java/jason/cli/mas/MasAppClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class MasAppClassLoader extends ClassLoader {
private List<String> paths;

public MasAppClassLoader(ClassLoader parent, List<String> paths) {

super(parent);
this.paths = paths;
}
Expand All @@ -20,7 +19,7 @@ public MasAppClassLoader(ClassLoader parent, List<String> paths) {
public Class loadClass(String name) throws ClassNotFoundException {
//System.out.println("Loading Class '" + name + "' ");

// CLILocalMAS must be loaded by this loader, so that classes latter loaded from it use this loader
// CLILocalMAS must be loaded by this loader, so that classes that will be loaded from it use this loader
if (name.equals(CLILocalMAS.class.getName())) {
Class<?> c = getJasonCLIClass();
// force this class to be assigned with this loader
Expand Down Expand Up @@ -67,17 +66,25 @@ private Class getJasonCLIClass() throws ClassNotFoundException {
}

private Class getAppClass(String name) throws ClassNotFoundException {
// TODO: consider proper application classpath
// .:bin/classes:build/classes/java/main:project classpath:*lib

for (String path: paths) {
try {
if (!path.isEmpty() && !path.endsWith("/"))
path += "/";
String file = path + name.replace('.', File.separatorChar) + ".class";
//System.out.println("try to load class "+name+" from "+file);
// This loads the byte code data from the file
var b = loadClassData(new FileInputStream(file));
byte[] b = null;
if (path.endsWith(".jar")) {
// load from jar
var file = //"jar:file:/Users/jomi/pro/jason-cli/build/libs/jason-cli-1.0-SNAPSHOT.jar!/jason/cli/mas/CLILocalMAS.class";
"jar:file:"+path+"!/"+
name.replace('.',File.separatorChar) + ".class";
System.out.println("try to load class "+name+" from "+file);
b = loadClassData(new URL(file).openStream());
System.out.println("OK "+name);
} else {
if (!path.isEmpty() && !path.endsWith("/"))
path += "/";
String file = path + name.replace('.', File.separatorChar) + ".class";
System.out.println("try to load class "+name+" from "+file);
// This loads the byte code data from the file
b = loadClassData(new FileInputStream(file));
}
if (b != null) {
return defineClass(name, b, 0, b.length);
}
Expand Down
12 changes: 11 additions & 1 deletion jason-cli/src/main/java/jason/cli/mas/StartMAS.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

Expand All @@ -34,7 +35,7 @@ public class StartMAS implements Runnable {

@Option(names = { "--env" }, defaultValue = "", paramLabel = "<env class>", description = "class that implements the environment and its arguments")
String envClass;
@Option(names = { "--cp" }, defaultValue = "", paramLabel = "<classpath>", description = "directories where java classes can be found (for environment implementation, for instance)")
@Option(names = { "--cp" }, defaultValue = "", paramLabel = "<classpath>", description = "directories where java classes can be found (e.g., for environment implementation)")
String classPathArg;

@Option(names = { "--mas2j" }, defaultValue = "", paramLabel = "<mas2j file>", description = "runs a Jason project")
Expand Down Expand Up @@ -124,6 +125,15 @@ public void run() {
classPathList.add(".");
classPathList.add("build/classes/java/main");
classPathList.add("bin/classes/");
File libs = new File("lib");
if (libs.exists()) {
for (var f: libs.list()) {
if (f.endsWith(".jar")) {
classPathList.add("lib/"+f);
}
}
}
//System.out.println("ClassPath="+classPathList);

try {
parent.parent.println("starting MAS "+masName+"...");
Expand Down
4 changes: 4 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
}
rootProject.name = 'jason'
include('jason-cli')
include('jason-interpreter')

0 comments on commit 6000f8c

Please sign in to comment.