File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Error trying to get a simple bare-bones package to work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error trying to get a simple bare-bones package to work." Watch "Error trying to get a simple bare-bones package to work." New topic
Author

Error trying to get a simple bare-bones package to work.

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Earlier Today I was able to create a package, with a class, that had a method in it, and access the method from another program.
Now I can not even get a very simple case of a package to work, at all.
Even when I go back to the old code that worked, it fails to allow me to compile the code that calls the method in the package.


Code in file myTestClass.java
---
package mytestpack;

import java.io.*;
import java.util.*;
import java.lang.String;

public class myTestClass
{
public static int myTestMethod()
{
System.out.println("hello World");
int i=0;
return i;
}
}
----
I then do this
javac -d . myTestClass.java

It compiles, and creates directory mytestpack
In the directory it places myTestClass.class

Then I write a simple program to call the method.
Code for testpack.java
----
import java.io.*;
import java.util.*;
import java.lang.String;
import mytestpack.*;

class testmypack
{
public static void main(String[] args)
{
int i;
i= myTestClass.myTestMethod();
}
}

---
I attempt to compile the above using this command:
javac testpack.java

when I try to compile the above, I get this error:
testpack.java:11: cannot access myTestClass
bad class file: ./myTestClass.java
file does not contain class myTestClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
i= myTestClass.myTestMethod();
^

Why is it even looking back at myTestClass.java, when I have already successfully compiled the class.
Would it not only need to access the myTestClass.class file?

The path to (and including) mytestpack is in my CLASSPATH.
(besides I get a different error, when it can't find the path).


What baffles me, is that I had this concept working earlier.
I am sure I used the same exact strategy as I show above.


If someone can tell me what I am doing wrong, it would be very much appreciated.
Thanks.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Because the method in the myTestClass is static , you need to do static imports.
Replace...
import mytestpack.*; by import static mytestpack.*;

Learn about static imports for more details


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
That didn't solve the problem.

I got exactly the same error.

Taking static out of the method definition didn't work either.

Earlier, when I had something like this working, I did use public static for the method definition, did not use static in the import statement and it worked.
Now I can't get anything to work at all (except putting the methods all inside the main class, that needs to use them).
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

James Tharakan wrote:Because the method in the myTestClass is static , you need to do static imports.
Replace...
import mytestpack.*; by import static mytestpack.*;

Learn about static imports for more details


Static imports where only needed if you did not want to use the fully qualified class name...
Say PI instead of Math.PI. If you are using fully qualified class names you shouldn't really need static imports..

More ever Sun doesn't seem to recommend static imports anyways .... http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

As for the original question ... Something look wrong with class paths to me though I can't quite place it yet.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Ok I stumped ... I've benn trying this out for 10 mins and its just not working from the command line. I put in in my eclipse IDE and it worked like a charm - so nothing wrong with the code.

Looking forward to a see whats going wrong here - Everything looks perfect too me
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Did you try the actual code in command line with static imports???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Try adding the -d and . to every javac call.
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
This is very strange.

I am now at work, and the same code that failed to compile using putty at home, is comiling without the error, on the unix workstation in the office.

Before when I said an example that had worked earlier for me, had just quit working; was working on my system at work, not at home.
I had then gone home, and continued working on it, when it started failing.

So what would cause this? (I guess we are getting beyond beginner here).

I have several java servlets, on the same server, that compile and run just fine, from my home system.
I don't see how this can be a firewall issue, but at the same time can not think of anything else.
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Another interesting symptom.

I get the same problem, when I compile from my PC, at work, using putty.
But it works using the unix workstation.

I get the error when I compile using reflection, on my PC as well.

Very very strange.
Any ideas on what would cause this?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Eileen:

I got your program to work by changing the import in the second class from

to


I'm not sure why it works, but it worked for me. BTW, you don't have to do includes for java.lang.* classes, since they are included automatically.

John.
james dunster
Greenhorn

Joined: May 03, 2009
Posts: 20





eileen keeney wrote:Earlier Today I was able to create a package, with a class, that had a method in it, and access the method from another program.
Now I can not even get a very simple case of a package to work, at all.
Even when I go back to the old code that worked, it fails to allow me to compile the code that calls the method in the package.


Code in file myTestClass.java
---
package mytestpack;

import java.io.*;
import java.util.*;
import java.lang.String;

public class myTestClass
{
public static int myTestMethod()
{
System.out.println("hello World");
int i=0;
return i;
}
}
----
I then do this
javac -d . myTestClass.java

It compiles, and creates directory mytestpack
In the directory it places myTestClass.class

Then I write a simple program to call the method.
Code for testpack.java
----
import java.io.*;
import java.util.*;
import java.lang.String;
import mytestpack.*;

class testmypack <-------------------------------------<< ??
{
public static void main(String[] args)
{
int i;
i= myTestClass.myTestMethod();
}
}

---
I attempt to compile the above using this command:
javac testpack.java

when I try to compile the above, I get this error:
testpack.java:11: cannot access myTestClass
bad class file: ./myTestClass.java
file does not contain class myTestClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
i= myTestClass.myTestMethod();
^

Why is it even looking back at myTestClass.java, when I have already successfully compiled the class.
Would it not only need to access the myTestClass.class file?

The path to (and including) mytestpack is in my CLASSPATH.
(besides I get a different error, when it can't find the path).


What baffles me, is that I had this concept working earlier.
I am sure I used the same exact strategy as I show above.


If someone can tell me what I am doing wrong, it would be very much appreciated.
Thanks.

What is testmypack?



eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
John de Michele wrote:Eileen:

I got your program to work by changing the import in the second class from

to


I'm not sure why it works, but it worked for me. BTW, you don't have to do includes for java.lang.* classes, since they are included automatically.

John.


I was pretty certain I had tested that before with no luck, but I tried it just now, and it seems to work.

This does not explain the difference in behavior I am seeing between the use of javac when I am using putty (or reflection) versus the use of javac from a unix workstation.
But it does solve the issue, and get my code working.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error trying to get a simple bare-bones package to work.