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.
Related
I'm learning Java and experimenting with Javafx in netbeans.
I am running the sqlite tutorial here: http://www.tutorialspoint.com/sqlite/sqlite_java.htm
When set up as a lone-file it works fine of course.
I'm setting it up in a test project "testDB" and for some reason when I initiate the class the class itself is recognized, but main() is not running.
Here is the testdb file itself:
testDB.java:
/*
* 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 sqlitetest;
import java.sql.*;
/**
*
*/
public class testDB {
public static void main(String args[]) {
//THESE STEPS ARE ON NOT RUNNING (compiles without errors)
System.out.println("testing");
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
public void makeStuff(){
}
}
sqlitetest.java
/*
* 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 sqlitetest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
*/
public class Sqlitetest extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
testDB test = new testDB();
test.makeStuff();
launch(args);
}
}
I think you are getting confused between a constructor and a main method.
A main method is only invoked when you start the JVM, running that specific class (or if you invoke it explicitly elsewhere).
A constructor is invoked when you create an instance of the class, like you are doing here.
In testdb, change:
public static void main(String args[]) {
to
public testdb() {
Alternatively, invoke testdb.main(args) (or with some other parameter) in Sqlitetest.main.
Anyone with experience using Java-Sandbox, I have implemented one of the basic examples found in the documentation but i cant get it working.
Code:
SandPlayground.java
import java.util.concurrent.TimeUnit;
import net.datenwerke.sandbox.*;
import net.datenwerke.sandbox.SandboxContext.AccessType;
import net.datenwerke.sandbox.SandboxContext.RuntimeMode;
import net.datenwerke.sandbox.SandboxedEnvironment;
public class SandPlayground {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("Running...");
SandboxService sandboxService = SandboxServiceImpl.initLocalSandboxService();
// configure context
SandboxContext context = new SandboxContext();
//context.setRunRemote(true);
context.setRunInThread(true);
context.setMaximumRunTime(2, TimeUnit.SECONDS, RuntimeMode.ABSOLUTE_TIME);
context.addClassPermission(AccessType.PERMIT, "java.lang.System");
context.addClassPermission(AccessType.PERMIT, "java.io.PrintStream");
//run code in sandbox
SandboxedCallResult<String> result = sandboxService.runSandboxed(MyEnvironment.class, context, "This is some value");
// output result
System.out.println(result.get());
}
}
MyEnvironment.java
import net.datenwerke.sandbox.SandboxedEnvironment;
public class MyEnvironment implements SandboxedEnvironment<String> {
private final String myValue;
public MyEnvironment(String myValue){
this.myValue = myValue;
}
#Override
public String execute() throws Exception {
/* run untrusted code */
System.out.println(myValue);
/* return some value */
return "This is a different value";
}
}
And I'm getting the error:
EDIT: I've included the dependencies, but I'm still getting some errors:
With the code above I get:
Exception in thread "main" net.datenwerke.sandbox.exception.SandboxedTaskKilledException: killed task as maxmimum runtime was exceeded
at net.datenwerke.sandbox.SandboxMonitorDaemon.testRuntime(SandboxMonitorDaemon.java:82)
at net.datenwerke.sandbox.SandboxMonitorDaemon.run(SandboxMonitorDaemon.java:57)
at java.lang.Thread.run(Thread.java:724)
and when i remove the context.setMaximumRunTime() call, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/IdentityMap ...
Any help is much appreciated.
most likely you are missing the javassist library (see the documentatio of the sandbox for dependencies: http://blog.datenwerke.net/p/the-java-sandbox.html). You'll find the javassist library on sourceforge at: https://sourceforge.net/projects/jboss/files/Javassist/
The javaassist library is used to remove finalizers in loaded code. This can be turned off in the sandbox context:
contex.setRemoveFinalizers(false)
Hope this helps.
I am trying to run the below code in Eclipse.
It's giving me an error "Editor does not contain a main type" but it does as you can see public static void main(String args[]).
Anyone know how to run this or why it does not recognize the main method?
package org.axiondb.functional;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* #version $Revision: 1.26 $ $Date: 2005/05/03 18:02:23 $
* #author Rodney Waldhoff
* #author Chuck Burdick
*/
public class TestAll extends TestCase {
public TestAll(String testName) {
super(testName);
}
public static void main(String args[]) {
String[] testCaseName = { TestAll.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestDatatypes.suite());
suite.addTest(TestDDL.suite());
suite.addTest(TestDQL.suite());
suite.addTest(TestDQLMisc.suite());
suite.addTest(TestDQLDisk.suite());
suite.addTest(TestDQLWithArrayIndex.suite());
suite.addTest(TestDQLDiskWithArrayIndex.suite());
suite.addTest(TestDQLWithBTreeIndex.suite());
suite.addTest(TestDQLDiskWithBTreeIndex.suite());
suite.addTest(TestDML.suite());
suite.addTest(TestDMLMisc.suite());
suite.addTest(TestDMLDisk.suite());
suite.addTest(TestDMLWithArrayIndex.suite());
suite.addTest(TestDMLDiskWithArrayIndex.suite());
suite.addTest(TestDMLWithBTreeIndex.suite());
suite.addTest(TestDMLDiskWithBTreeIndex.suite());
suite.addTest(TestMemoryClob.suite());
suite.addTest(TestDiskClob.suite());
suite.addTest(TestMemoryBlob.suite());
suite.addTest(TestDiskBlob.suite());
suite.addTest(TestThreadedSelect.suite());
suite.addTest(TestIndexedJoin.suite());
suite.addTest(TestBugs.suite());
suite.addTest(TestAxionBTreeDelete.suite());
suite.addTest(TestSpecials.suite());
suite.addTest(TestFunctions.suite());
suite.addTest(TestThreadedDML.suite());
suite.addTest(TestTransactions.suite());
suite.addTest(TestTransactionsDisk.suite());
suite.addTest(TestConstraints.suite());
suite.addTest(TestBooleanLiterals.suite());
suite.addTest(TestTransactionalLobs.suite());
suite.addTest(TestTransactionalLobsDisk.suite());
suite.addTest(TestMetaData.suite());
suite.addTest(TestMetaDataDisk.suite());
suite.addTest(TestDatabaseLock.suite());
suite.addTest(TestIndexSpecials.suite());
suite.addTest(TestForElmar.suite());
suite.addTest(TestPrepareStatement.suite());
suite.addTest(TestGroupByAndOrderBy.suite());
suite.addTest(TestBinaryStream.suite());
return suite;
}
}
Try reopening the file, then run the test.
Otherwise, restart Eclipse. please let me know if this solves the problem! or else give me more details and I'll try and help.
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 ...
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.