Why isn't this Java jar working? - java

When I run the code below I get the following error.
C:\Documents and Settings\BOS\Desktop\test>java -jar test.jar
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I've got these files in \test directory = crimson.jar robosuite-api.jar and test.jar.
Here is the example they give to launch a robot?
import com.kapowtech.robosuite.api.java.rql.*;
public class SimpleRunRobot {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: RunRobot <robotURL>");
System.exit(1);
}
try {
// Run the robot
RQLResult result =
RobotExecutor.getRobotExecutor().execute(args[0]);
// Output the results
System.out.println(result);
}
catch (RQLException e) {
System.out.println("An error occurred: " + e);
}
}
}
Why is this giving me that Unknown Source error?
package robosuite.robots;
import com.kapowtech.robosuite.api.java.rql.RQLException;
import com.kapowtech.robosuite.api.java.rql.RQLResult;
import com.kapowtech.robosuite.api.java.rql.RobotExecutor;
import com.kapowtech.robosuite.api.java.rql.construct.RQLObjects;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* <p>
* This is an autogenerated class. It has been generated from the
* <code>library:/test.robot</code> file.
*
* #author RoboSuite
*/
public class Test {
// ----------------------------------------------------------------------
// Class fields
// ----------------------------------------------------------------------
private static final String ROBOT_URL = "library:/test.robot";
private static final RobotExecutor ROBOT_EXECUTOR = RobotExecutor.getRobotExecutor(SingletonRQLEngine.getInstance());
private static final Converter CONVERTER = Converter.getInstance();
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
/**
* Creates a new Test instance that can be used to execute the
* <code>library:/test.robot</code>.
*/
public Test() {
}
// ----------------------------------------------------------------------
// Instance methods
// ----------------------------------------------------------------------
/**
* Executes this robot.
*
* #param test an input object to the robot.
* #return an array of output objects.
* #throws java.io.IOException if the execution fails for some reason.
*/
public Testst[] run(Test0 test) throws java.io.IOException {
try {
// Prepare input objects
List parameters = new ArrayList();
parameters.add(test);
RQLObjects inputObjects = CONVERTER.convertBeansToRQLObjects(parameters);
// Run robot
RQLResult rqlResult = ROBOT_EXECUTOR.execute(ROBOT_URL, inputObjects);
// Extract output objects
RQLObjects outputObjects = rqlResult.getOutputObjects();
List result = CONVERTER.convertRQLObjectsToBeans(outputObjects);
return (Testst[]) result.toArray(new Testst[result.size()]);
} catch (RQLException e) {
throw new IOException(e.toString());
}
}
/* ------------------------------------------------------------------- */
}

If your using Java 7, Read this.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922

Try
java -cp test.jar
include your other .jar files also
If you are using a manifest file make sure you have defined your main class.
for e.g.
Main-Class: test.MyApp

You have to add the name of the class having main() method in META-INF/manifest file.
Here is the link with more information :
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
Thanks.

Why is this giving me that Unknown Source error?
The "unknown source" messages are not an error. It is the JVM telling you that the code that you are executing was compiled without any debug information; e.g. with the -gLnone option. As a result, the source file names and line numbers that would normally be included in the stacktrace are not available.
In this case, the code is some platform specific stuff that is internal to the JVM. Don't worry about it ...

Related

serial port how do I?

Hi I am using Netbeans as my IDE. I want to play a little bit with the serial port. I am using an FTDI cable in my laptop that converts usb port to RS232 serial port.
I have found these interesting sites:
http://www.embeddedunveiled.com/
https://github.com/RishiGupta12/serial-communication-manager
I have written this piece of code taken from first link
under
Example usage
•How to find serial ports available on system is here.
code:
package findserialports;
import com.embeddedunveiled.serial.SerialComManager;
/**
*
* #author Alexandros
*/
public class FindSerialPorts {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
String[] ports = scm.listAvailableComPorts();
for(String port: ports){
System.out.println(port);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
There is an error in code in statement
import com.embeddedunveiled.serial.SerialComManager;
Also it says on site second link
that The folder prebuilt contains ready-to-use jar file (scm-1.0.3.jar) that can be imported in any project and referenced right away.
Where do I find the prebuild folder?
thanks
----------Second Post----------------------------------------------
Moving on I have following code:
package serialportftdi;
import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;
/**
*
* #author Alexandros
*/
public class SerialPortFTDI {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
long handle = scm.openComPort("/dev/ttyUSB1", true, true, false); scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
scm.writeString(handle, "testing hello", 0) == true);
String data = scm.readString(handle);
System.out.println("data read is :" + data);
scm.closeComPort(handle);
}catch (Exception e) {
e.printStackTrace();
}
}
}
An error occurs at line : scm.writeString(handle, "testing hello", 0) == true);
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: not a statement
scm.writeString(handle, "testing hello", 0) == true);
^
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: ';' expected
scm.writeString(handle, "testing hello", 0) == true);
^
2 errors
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 3 seconds)
Also I am trying to understand the code but I don't understand what 'handle' does. Is this a handle as in C++? If yes then what exactly does? I was trying to find info about it in Java Deitel and wrox books but no info.
Ok! I played a little bit with code and now it works fine. Bare in mind that the ftdi cable must be inserted to the usb port that is found as being connected to COM4 serial port.
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package serialportftdi;
import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;
/**
*
* #author Alexandros
*/
public class SerialPortFTDI {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
long handle = scm.openComPort("COM4", true, true, true);
scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
scm.writeString(handle, "testing hello", 0); //== true);
String data = scm.readString(handle);
System.out.println("data read is :" + data);
scm.closeComPort(handle);
}catch (Exception e) {
e.printStackTrace();
}
}
}

Java - Unable to create a file

I am in a Linux Envirnomnet. I am also using Netbeans. Here is my code below:
import java.io.*;
public class myFirstJavaProgram {
public static void main(String[] args) {
File file = new File("home/gk/Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
}
}
You forgot a slash before home. It is looking for a folder that most likely does not exist inside the classpath.
EDIT
After you pointed out the exception you were receiving I realized that a checked exception is not being handled. You need to catch the possible IOException or include the exception in the method signature.
import java.io.*;
/**
*
* #author Ashwin Parmar
*/
public class myFirstJavaProgram {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
File file = new File("/home/gk/Hello1.txt");
file.createNewFile();
} catch(IOException e) {
System.out.println(e.getMessage());
}
// creates a FileWriter Object
}
}
When dealing with any File IO action in Java, it is always best to use a try/catch loop
Error in your path.
This home/gk/Hello1.txt should be /home/gk/Hello1.txt

Start/Stop services using JNA

I am writing a utility to start and stop windows services. The program will be distributed across many computers with differing levels of user privileges so I don't want to use the command line. I've tried using JNA,
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* #author
*/
public class WindowsServices {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try
{
// TODO code application logic here
W32ServiceManager serviceManager = new W32ServiceManager();
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SERVICE_ACCEPT_STOP);
service.stopService();
service.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
When I run the program I get the following error
com.sun.jna.platform.win32.Win32Exception: The handle is invalid.
at com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77)
at windowsservices.WindowsServices.main(WindowsServices.java:26)
Any suggestions would be most helpful.
Thanks for the suggestion the author of the question found the error.
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* #author
*/
public class WindowsServices {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try
{
W32ServiceManager serviceManager = new W32ServiceManager();
serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SC_MANAGER_ALL_ACCESS);
service.startService();
service.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
The error was that the code didn't open the Service Control Manager. I was looking on MSDN and found the process that I needed to follow. I also chanced the permission value, that might also of caused a failure.
We use Runtime.getRuntime().exec(command) and then execute the command
cmd /c net start
to start services and
cmd /c net stop
to stop services.
Of course you have to know the service names (and in our case it is DB2 we are after). But this has worked for us.

Error importing classes for RDF crawler

I'm using a rdf crawler, in that I had a class named as:
import edu.unika.aifb.rdf.crawler.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.FileManager;
These are class file termed as error, and I try with jena packages but I had attached, it does not make any changes.
Update:
Full SampleCrawl.java class content:
import java.util.*;
import edu.unika.aifb.rdf.crawler.*;
/**
* Call this class with 3 arguments - URL to crawl to,
* depth and time in seconds
*/
public class SampleCrawl {
/**
* #param uRI
* #param depth
* #param time
*/
#SuppressWarnings("rawtypes")
public SampleCrawl(Vector uRI, Vector hf, int depth, int time){
// Initialize Crawling parameters
CrawlConsole c = new CrawlConsole(uRI,hf,depth,time);
// get an ontology file from its local location
// (OPTIONAL)
c.setLocalNamespace("http://www.daml.org/2000/10/daml-ont","c:\\temp\\rdf\\schemas\\daml-ont.rdf");
// set all the paths to get all the results
c.setLogPath("c:\\temp\\crawllog.xml");
c.setCachePath("c:\\temp\\crawlcache.txt");
c.setModelPath("c:\\temp\\crawlmodel.rdf");
try{
// crawl and get RDF model
c.start();
// This writes all three result files out
c.writeResults();
}catch(Exception e){
}
}
/**
* #param args
* #throws Exception
*/
#SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err.println("Usage: java -cp [JARs] SampleCrawl [URL] [depth:int] [time:int]");
System.exit(0);
}
Vector uris = new Vector();
uris.add(args[0]);
// no host filtering - crawl to all hosts
Vector hostfilter = null;
/* You may want to do something else to enable host filtering:
* Vector hostfilter = new Vector();
* hostfilter.add("http://www.w3.org");
*/
int depth = 2;
int time = 60;
try {
depth = Integer.parseInt(args[1]);
time = Integer.parseInt(args[2]);
}
catch (Exception e) {
System.err.println("Illegal argument types:");
System.err.println("Argument list: URI:String depth:int time(s):int");
System.exit(0);
}
new SampleCrawl(uris,hostfilter,depth,time);
}
}
Question:
How to add import edu.unika.aifb.rdf.crawler.; error occurs here
I googled the package that you're trying to import, and it appears that you're using Kaon. Assuming that's so, you have made an error in your import declaration. You have:
import edu.unika.aifb.rdf.crawler.*;
whereas the download available on SourceForge would require:
import edu.unika.aifb.rdf.rdfcrawler.*;
As an aside, it would be helpful if you would include information, such as "I'm trying to use Kaon's rdfcrawler from ..." in your question. Otherwise, we have to try to guess important details in your setup.

Error in simple Java application

Just playing around with java trying to learn it etc.
Here is my code so far, using HtmlUnit.
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
/**
* #author
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("starting ");
Spider spider = new Spider();
spider.Test();
}
}
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
/**
* #author
*/
public class Spider {
public void Test() throws Exception
{
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");
System.out.println(page.getTitleText());
}
}
I am using Netbeans.
I can't seem to figure out what the problem is, why doesn't it compile?
The error:
C:\Users\mrblah\.netbeans\6.8\var\cache\executor-snippets\run.xml:45:
Cancelled by user.
BUILD FAILED (total time: 0 seconds)
The row in the xml is:
<translate-classpath classpath="${classpath}" targetProperty="classpath-translated" />
Test is declared to throw Exception. If you add "throws Exception" to your main method it should compile. For example:
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
System.out.println("starting ");
Spider spider = new Spider();
spider.Test();
}
What Steve said is correct. But maybe there are some problems with the uppercase character of Test. A method always starts with a lower case character. So test would be better.
Unchecking the "Compile on Save" option of the "Properties" tab in Netbeans 7.1.2 resolved a similar error message for me.

Categories

Resources