Trying to incluide solr server into my java project and starting it - java

I have a java project and i'm trying to include a solr server into my project and start it.
I copy de folders of the server into my eclipse structhure and start the server with Runtime.getRuntime().exec("path/solr.cmd start") but when I create the Runnable jar it doesn't work.
can you help me?
thanks
package prueba_solr;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import javax.swing.JOptionPane;
public class arrancar {
public static void main(String[] args) {
try {
// Arrancar el servidor Apache SOLR
arrancar();
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e);
}
}
private static void arrancar() {
System.out.println("Arrancando...");
String comando = "./bin/models/ir/bin/solr.cmd start"; // SI EN ECLIPSE. NO EN EL JAR
try {
JOptionPane.showMessageDialog(null,comando);
Runtime.getRuntime().exec(comando);
} catch (IOException e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e);
}
}
}

Related

Constantly receive java.lang.ClassNotFoundException

I am very new to Java and the RMI system. I am following a tutorial but I am unsure why I keep getting the following errors[1]
[1]: https://i.stack.imgur.com/xeYTn.png
I have attached the code (taken directly from the tutorial here: https://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html)
I have tried:
removing any lines with 'package'
changing classpath variables
reinstalling java and javac
setting classpath in the 'rmiregistry &' command
Any help would be appreciated
edit: forgot to attach the code.
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject((Remote) obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
You have to run the command rmiregistry & in the folder the code is compiled into. In this case, "files"
the "getting started" tutorial didn't mention that.

RandomAccessFile doesn't work with Minecraft Forge

I'm working at the moment on a Mod for Minecraft with a dedicated Gui system written in C++ and Qt5. I let my GUI and Minecraft communicate through a named pipe, but I have there a small problem. I can read and write with a simple Java and C++(Qt) program into the pipe. But when I create a new instance of my Pipeendpoint class in post init of Minecraft Forge it can't read anything from the Pipe. In a standalone system, it can read stuff.
Not working Forge Implementation:
package de.CoderDE.CodersAnimationEditor;
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
#SideOnly(Side.CLIENT)
public class ClientProxy extends CommonProxy {
static PipeEndpoint pendpoint;
#Override
public void preInit(FMLPreInitializationEvent e) {
super.preInit(e);
}
#Override
public void init(FMLInitializationEvent e) {
super.init(e);
}
#Override
public void postInit(FMLPostInitializationEvent e) {
super.postInit(e);
try {
pendpoint = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
Working standalone implementation:
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
public class Main {
static PipeEndpoint pipe;
public static void main(String[] args) {
try {
pipe = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And the important PipeEndpoint class:
package de.CoderDE.CodersAnimationEditor.Pipe;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class PipeEndpoint {
private Thread reciever;
private RandomAccessFile pipe;
public PipeEndpoint() throws FileNotFoundException {
pipe = new RandomAccessFile("\\\\.\\pipe\\CodersAnimationEditor", "rw");
reciever = new Thread(new PipeEndpointReciever());
reciever.start();
}
private class PipeEndpointReciever implements Runnable {
#Override
public void run() {
try {
while (true) {
System.out.print((char)pipe.read());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And with "can't read anything" I mean that it never returns from pipe.read().
Oh, and the Java application starts after the C++(Qt) LocalServer started listening and waits for a new connection.

error trying to attach to virtual machine

I'm trying to attach to a running JVM to debug it using the Virtual Machine class in java. I'm currently have java 8, with jdk1.8.0_11. I have tried to add a manifest to it, with but to no avail. I'm also importing the tools.jar file from my JDK\libs folder.
My code:
import java.io.IOException;
import java.util.List;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
public class loadVM {
public static void main(String[] args) {
String name = "replaceAfterNameFound";
List <VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor vmd: vms) {
System.out.println(vmd.displayName());
if (vmd.displayName().equals(name)) {
try {
VirtualMachine vm = VirtualMachine.attach(vmd.id());
String agent = "";
vm.loadAgent(agent);
} catch(AttachNotSupportedException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(AgentLoadException e) {
e.printStackTrace();
} catch(AgentInitializationException e) {
e.printStackTrace();
}
}
}
}
}
Here is a copy of the error I get when running it:
java.util.ServiceConfigurationError:com.sun.tools.attach.spi.AttachProvider:
Provider sun.tools.attach.WindowsAttachProvider could not be
instantiated
Thank you guys for all your help!

How to Generate Dynamic Reports Java

I tried to create a Dynamic-reports but Error occured.Anybody give some solution for create Dynamic Reports java.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;
public class NewClass {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sts","john", "pswrd");
} catch (SQLException e) {
e.printStackTrace();
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
JasperReportBuilder report = DynamicReports.report();//a new report
report
.columns(
Columns.column("Customer Id", "customerid", DataTypes.integerType()),
Columns.column("Name", "customername", DataTypes.stringType()),
Columns.column("Address", "address", DataTypes.stringType()),
Columns.column("Date", "dates", DataTypes.dateType()))
.title(//title of the report
Components.text("SimpleReportExample")
.setHorizontalAlignment(HorizontalAlignment.CENTER))
.pageFooter(Components.pageXofY())//show page number on the page footer
.setDataSource("SELECT customerid, customername,address,dates FROM customersdetails",
connection);
try {
//show the report
report.show();
//export the report to a pdf file
report.toPdf(new FileOutputStream("D:/report.pdf"));
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
I tried to create a dynamic-reports but error occured.
Exception:
Exception in thread "main" java.lang.NoSuchMethodError: net.sf.jasperreports.engine.util.JRLoader.getLocationInputStream(Ljava/lang/String;)Ljava/io/InputStream;
at net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:99) at net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:99)
at net.sf.jasperreports.engine.DefaultJasperReportsContext.initProperties(DefaultJasperReportsContext.java:94)
at net.sf.jasperreports.engine.DefaultJasperReportsContext.<init>(DefaultJasperReportsContext.java:71)
at net.sf.jasperreports.engine.DefaultJasperReportsContext.<clinit>(DefaultJasperReportsContext.java:59)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:95)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:71)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFontHeight(StyleResolver.java:52)
at net.sf.dynamicreports.design.transformation.TemplateTransform.getTextFieldHeight(TemplateTransform.java:981)
at net.sf.dynamicreports.design.transformation.ComponentTransform.textField(ComponentTransform.java:334)
at net.sf.dynamicreports.design.transformation.ComponentTransform.component(ComponentTransform.java:154)
at net.sf.dynamicreports.design.transformation.ComponentTransform.list(ComponentTransform.java:287)
at net.sf.dynamicreports.design.transformation.BandTransform.band(BandTransform.java:184)
at net.sf.dynamicreports.design.transformation.BandTransform.transform(BandTransform.java:74)
at net.sf.dynamicreports.design.base.DRDesignReport.transform(DRDesignReport.java:136)
at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:108)
at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:100)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:261)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:288)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:299)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperReportBuilder.java:328)
at Robb.NewClass.main(NewClass.java:60)
You need to add jar in your project

JavaRMI connection to localhost failure

I´m starting with RMI.
I wan´t to create a server in Java for a database manger (Is a college thing) and when I run the code listed below, to start the interface for the server, i have a connexion failure with localhost:
By the way, Im Using OS X and Eclipse to code.
/**
*
*/
package es.uned.database;
import es.uned.common.IBasededatos;
import es.uned.common.Utils;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.*;
import es.uned.common.IServidor;
/**
*
* #author
*
*/
public class Basededatos extends UnicastRemoteObject implements IBasededatos{
protected Basededatos() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws Exception {
Utils.setCodeBase(IServidor.class);
if (System.getSecurityManager()==null) {
System.setSecurityManager(new RMISecurityManager());
}
String nombre = "//localhost/Basededatos";
try{
IBasededatos servidorBD = new Basededatos();
Naming.rebind(nombre, servidorBD);
System.out.println("Data base is up an ready");
}catch (Exception e) {
System.err.println("Database exception: " + e.getMessage());
e.printStackTrace();
}
}
}
This is the error message I get.
access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
I´ve tried this other code, but with the same success
/**
*
*/
package es.uned.database;
import es.uned.common.IBasededatos;
import es.uned.common.Utils;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
/**
*
* #author
*
*/
public class Basededatos implements IBasededatos{
public Basededatos() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws Exception {
//Utils.setCodeBase(IServidor.class);
if (System.getSecurityManager()==null) {
System.setSecurityManager(new RMISecurityManager());
}
String nombre = "//localhost/Basededatos";
try{
IBasededatos servidorBD = new Basededatos();
IBasededatos stub = (IBasededatos)UnicastRemoteObject.exportObject(servidorBD, 1099);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(nombre, servidorBD);
System.out.println("Servidor Base de datos arriba");
}catch (Exception e) {
System.err.println("Excepcion base de datos: " + e.getMessage());
e.printStackTrace();
}
}
}
Can anybody help me?
Thanks a lot
The client is getting the Registry from the wrong place. The Registry it needs to lookup is at the server host, not its own localhost.
RMI URLs are for use with the Naming class. You're using the Registry interface in the client so you shouldn't be specifying //localhost/ at all.

Categories