Skip to main content
added 10 characters in body
Source Link

I have a jar file so I tried to run the class from Mathematica following these steps https://reference.wolfram.com/language/workflow/ConnectToAnExternalJavaProgram.htmlthese steps, but I get all these:

Java::excptn: A Java exception occurred: java.lang.ClassNotFoundException: Buscar
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398).

LoadJavaClass::fail: Java failed to load class Buscar.
 
Needs["JLink`"]
InstallJava[]
AddToClassPath["C:\Users\Amada\Documents\Buscar.java"]
LoadJavaClass["Buscar"]
 

And this is the java code,Java codel I made it with Apache-Neatbeans 11.2 and I have JDK 12 and Mathematica 12 (if it's useful)

package buscar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Buscar {

    public static void main(String[] args) {
        buscador("issue");
    }
    
    public static void buscador(String palabra){
     Scanner entrada= null;
        String linea;
        int numdelinea= 1;
        boolean contiene= false;
        Scanner sc = new Scanner(System.in);
        
         try{
            
            File f= new File("C:/Program Files/Wolfram Research/Mathematica/12.1/Documentation/English/System/ExampleData/USConstitution.txt");
            entrada= new Scanner(f);
            System.out.println("Archivo: " + f.getName());
            System.out.println("Texto a bucar: " + palabra);
            
            while(entrada.hasNext()){
                linea= entrada.nextLine();
                if(linea.contains(palabra)){
                    System.out.println("Línea " + numdelinea + ": " + linea);
                    contiene = true;
                }
                numdelinea++;
            }
            if( !contiene){
                System.out.println(palabra + "no se ha encontrado");
            }
        } catch (FileNotFoundException e){
            System.out.println(e.toString());
        }catch (NullPointerException e){
            System.out.println(e.toString()+ "No se ha seleccionado ningún archivo");
        }catch (Exception e){
            System.out.println(e.toString());
        }finally{
            if(entrada !=null){
                entrada.close();
                }
            }
        }
    }

package buscar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Buscar {

    public static void main(String[] args) {
        buscador("issue");
    }
    
    public static void buscador(String palabra){
     Scanner entrada= null;
        String linea;
        int numdelinea= 1;
        boolean contiene= false;
        Scanner sc = new Scanner(System.in);
        
         try{
            
            File f= new File("C:/Program Files/Wolfram Research/Mathematica/12.1/Documentation/English/System/ExampleData/USConstitution.txt");
            entrada= new Scanner(f);
            System.out.println("Archivo: " + f.getName());
            System.out.println("Texto a bucar: " + palabra);
            
            while(entrada.hasNext()){
                linea= entrada.nextLine();
                if(linea.contains(palabra)){
                    System.out.println("Línea " + numdelinea + ": " + linea);
                    contiene = true;
                }
                numdelinea++;
            }
            if( !contiene){
                System.out.println(palabra + "no se ha encontrado");
            }
        } catch (FileNotFoundException e){
            System.out.println(e.toString());
        }catch (NullPointerException e){
            System.out.println(e.toString()+ "No se ha seleccionado ningún archivo");
        }catch (Exception e){
            System.out.println(e.toString());
        }finally{
            if(entrada !=null){
                entrada.close();
                }
            }
        }
    }

I have a jar file so I tried to run the class from Mathematica following these steps https://reference.wolfram.com/language/workflow/ConnectToAnExternalJavaProgram.html, but I get all these:

Java::excptn: A Java exception occurred: java.lang.ClassNotFoundException: Buscar
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398).

LoadJavaClass::fail: Java failed to load class Buscar.
 
Needs["JLink`"]
InstallJava[]
AddToClassPath["C:\Users\Amada\Documents\Buscar.java"]
LoadJavaClass["Buscar"]
 

And this the java code, I made it with Apache-Neatbeans 11.2 and I have JDK 12 and Mathematica 12 (if it's useful)

package buscar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Buscar {

    public static void main(String[] args) {
        buscador("issue");
    }
    
    public static void buscador(String palabra){
     Scanner entrada= null;
        String linea;
        int numdelinea= 1;
        boolean contiene= false;
        Scanner sc = new Scanner(System.in);
        
         try{
            
            File f= new File("C:/Program Files/Wolfram Research/Mathematica/12.1/Documentation/English/System/ExampleData/USConstitution.txt");
            entrada= new Scanner(f);
            System.out.println("Archivo: " + f.getName());
            System.out.println("Texto a bucar: " + palabra);
            
            while(entrada.hasNext()){
                linea= entrada.nextLine();
                if(linea.contains(palabra)){
                    System.out.println("Línea " + numdelinea + ": " + linea);
                    contiene = true;
                }
                numdelinea++;
            }
            if( !contiene){
                System.out.println(palabra + "no se ha encontrado");
            }
        } catch (FileNotFoundException e){
            System.out.println(e.toString());
        }catch (NullPointerException e){
            System.out.println(e.toString()+ "No se ha seleccionado ningún archivo");
        }catch (Exception e){
            System.out.println(e.toString());
        }finally{
            if(entrada !=null){
                entrada.close();
                }
            }
        }
    }

I have a jar file so I tried to run the class from Mathematica following these steps, but I get all these:

Java::excptn: A Java exception occurred: java.lang.ClassNotFoundException: Buscar
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398).

LoadJavaClass::fail: Java failed to load class Buscar.
Needs["JLink`"]
InstallJava[]
AddToClassPath["C:\Users\Amada\Documents\Buscar.java"]
LoadJavaClass["Buscar"]

And this is the Java codel I made it with Apache-Neatbeans 11.2 and I have JDK 12 and Mathematica 12 (if it's useful)

package buscar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Buscar {

    public static void main(String[] args) {
        buscador("issue");
    }
    
    public static void buscador(String palabra){
     Scanner entrada= null;
        String linea;
        int numdelinea= 1;
        boolean contiene= false;
        Scanner sc = new Scanner(System.in);
        
         try{
            
            File f= new File("C:/Program Files/Wolfram Research/Mathematica/12.1/Documentation/English/System/ExampleData/USConstitution.txt");
            entrada= new Scanner(f);
            System.out.println("Archivo: " + f.getName());
            System.out.println("Texto a bucar: " + palabra);
            
            while(entrada.hasNext()){
                linea= entrada.nextLine();
                if(linea.contains(palabra)){
                    System.out.println("Línea " + numdelinea + ": " + linea);
                    contiene = true;
                }
                numdelinea++;
            }
            if( !contiene){
                System.out.println(palabra + "no se ha encontrado");
            }
        } catch (FileNotFoundException e){
            System.out.println(e.toString());
        }catch (NullPointerException e){
            System.out.println(e.toString()+ "No se ha seleccionado ningún archivo");
        }catch (Exception e){
            System.out.println(e.toString());
        }finally{
            if(entrada !=null){
                entrada.close();
                }
            }
        }
    }

Source Link

Problems when importing a custom Java class

I have a jar file so I tried to run the class from Mathematica following these steps https://reference.wolfram.com/language/workflow/ConnectToAnExternalJavaProgram.html, but I get all these:

Java::excptn: A Java exception occurred: java.lang.ClassNotFoundException: Buscar
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398).

LoadJavaClass::fail: Java failed to load class Buscar.

This is the code in my notebook

Needs["JLink`"]
InstallJava[]
AddToClassPath["C:\Users\Amada\Documents\Buscar.java"]
LoadJavaClass["Buscar"]

And this the java code, I made it with Apache-Neatbeans 11.2 and I have JDK 12 and Mathematica 12 (if it's useful)

package buscar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Buscar {

    public static void main(String[] args) {
        buscador("issue");
    }
    
    public static void buscador(String palabra){
     Scanner entrada= null;
        String linea;
        int numdelinea= 1;
        boolean contiene= false;
        Scanner sc = new Scanner(System.in);
        
         try{
            
            File f= new File("C:/Program Files/Wolfram Research/Mathematica/12.1/Documentation/English/System/ExampleData/USConstitution.txt");
            entrada= new Scanner(f);
            System.out.println("Archivo: " + f.getName());
            System.out.println("Texto a bucar: " + palabra);
            
            while(entrada.hasNext()){
                linea= entrada.nextLine();
                if(linea.contains(palabra)){
                    System.out.println("Línea " + numdelinea + ": " + linea);
                    contiene = true;
                }
                numdelinea++;
            }
            if( !contiene){
                System.out.println(palabra + "no se ha encontrado");
            }
        } catch (FileNotFoundException e){
            System.out.println(e.toString());
        }catch (NullPointerException e){
            System.out.println(e.toString()+ "No se ha seleccionado ningún archivo");
        }catch (Exception e){
            System.out.println(e.toString());
        }finally{
            if(entrada !=null){
                entrada.close();
                }
            }
        }
    }

Thank you everyone.

-Luisa